You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何判断点击的.add_your_qora元素是否同时拥有myclass类?

Checking if a clicked .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:

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:22:20