如何判断点击的.add_your_qora元素是否同时拥有myclass类?
.add_your_qora element has the myclass class in jQuery Hey Martin, great question! Checking for an additional class on a clicked element is straightforward with jQuery—here are two solid ways to handle this:
1. Use hasClass() (Recommended for Simple Class Checks)
jQuery’s hasClass() method is purpose-built for this exact scenario: it returns a boolean (true or false) indicating whether the element has the specified class.
Update your code like this:
doc.on("click", ".add_your_qora", function(e){ // Check if the clicked element has "myclass" if ($(this).hasClass("myclass")) { // Your logic when the class exists console.log("Clicked element has 'myclass'!"); } else { // Your logic when the class doesn't exist console.log("Clicked element doesn't have 'myclass'."); } });
$(this) refers directly to the specific .add_your_qora element that was clicked, so we’re checking exactly the element the user interacted with.
2. Use is() (Flexible for Complex Selectors)
If you ever need to check for more complex conditions (like combining multiple classes, or matching other selector types), is() is a great alternative. For your use case, it would look like this:
doc.on("click", ".add_your_qora", function(e){ if ($(this).is(".myclass")) { // Your logic here } });
is() checks if the element matches the given selector—so passing .myclass will return true if the element has that class.
Between the two, hasClass() is more readable for simple class checks like yours, but both will get the job done!
内容的提问来源于stack exchange,提问作者Martin AJ

