jquery-当对象没有类时如何不触发事件
我想在以下条件下执行此操作:jquery-当对象没有类时如何不触发事件,jquery,html,css,Jquery,Html,Css,我想在以下条件下执行此操作: if ( this div has NO class firing on) { // do nothing when click on it } // else do something 可能吗 我倾向于这样做,因为这是与另一个学生团队合作编码的限制。但是,如果我的问题让人困惑和不清楚,请原谅。我不确定我是否正确解释了您的问题,但我相信只有在触发事件的元素具有特定类时,您才希望执行特定回调。在这种情况下,您可以在回调中执行以下操作: $("
if ( this div has NO class firing on) {
// do nothing when click on it
} // else do something
可能吗
我倾向于这样做,因为这是与另一个学生团队合作编码的限制。但是,如果我的问题让人困惑和不清楚,请原谅。我不确定我是否正确解释了您的问题,但我相信只有在触发事件的元素具有特定类时,您才希望执行特定回调。在这种情况下,您可以在回调中执行以下操作:
$("#someId").on("click", function () {
if (!$(this).hasClass("someClass"))
return false;
// Do your stuff here, if we get here, the element has the desired class
});
使用此解决方案,您将捕获事件,但如果元素没有您感兴趣的类,您将不做任何事情并尽早退出回调
更新:
如果您没有特别寻找任何类,只是想确保它根本没有任何类,那么您可以这样做:
$("#someId").on("click", function () {
if ($(this).prop("class").length === 0)
return false;
// Do your stuff here, if we get here, the element has no class
});
你做得不对。
将类添加到div中,然后您可以使用选择器或hasClass。这是一个非常模糊的问题描述,也是一个非常一般的答案:
$('div').each(function() {
if (!this.hasAttribute('class')) { //has no class
$(this).off(); //requires the use of on(), but removes all event handlers
}
$(this).on('click', ...)
});
您可以委托并检查元素是否有类(而不是空类)——现在它只会在div有类时触发
$('parentelement').on('click','div:not([class=""])[class]',function(){
});
我不明白你的问题。但我想你想要这样的东西
if($(this).hasClass('anyClass')){
//do nothing
return false;
}else{
//Do something
}
注意:您试图以错误的方式完成它。试着找出一些替代方法。太好了!非常感谢你的回答,尽管我的问题很难回答。