我想这样做的条件:的jQuery - 如何不触发事件时,对象没有类
if (this div has NO class firing on) {
// do nothing when click on it
} // else do something
这可能吗?
我倾向于这样做,因为它是与另一个学生团队合作的编码约束。但是,如果这个问题令人困惑和不清楚,我很抱歉。
我想这样做的条件:的jQuery - 如何不触发事件时,对象没有类
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
}
注意:您试图以错误的方式完成它。尝试找出一些替代方法。
完美!非常感谢你的回答,虽然我的问题太固执了。 – 2013-02-15 15:59:11
@AjarnCanz我的荣幸! – 2013-02-15 16:02:06