jQuery:如何查找*没有*某个类的元素

2024-01-10

为什么这个会失败...

$( 'div.contactAperson input' ).not( 'input.hadFocus' ).focus(function() {
    $(this).attr('value', '' );
});

...它的目的是嗅出具有的输入not获得类 .hadFocus ,然后当该子集之一接收焦点时,它应该将该值设置为 null 。

现在,输入值总是被修改——测试 .not( 'input.hadFocus' ) 无法停止执行。

顺便说一句,在上面的代码之前是以下代码,它工作正常:

$( 'div.contactAperson input' ).focus(function() {
    $( this ).addClass( 'hadFocus' );
});

感谢您的聪明才智 - 干杯, - 艾伦


您需要根据元素的当前状态运行处理程序 - 不是绑定时的状态。您可能需要使用live http://api.jquery.com/live/捆绑。

尝试这样的事情:

$('div.contactAperson input:not(.hadFocus)').live('focus', function() {
    $(this).attr('value', '' );
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

jQuery:如何查找*没有*某个类的元素 的相关文章

随机推荐