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

Ala*_*lan 6 jquery-selectors

为什么这会失败......

$( 'div.contactAperson input' ).not( 'input.hadFocus' ).focus(function() {
    $(this).attr('value', '' );
});
Run Code Online (Sandbox Code Playgroud)

...它的意思是嗅出没有类.hadFocus的输入,然后当其中一个子集获得焦点时,它应该将值变为null.

现在,输入值总是被破坏 - 测试.not('input.hadFocus')无法停止执行.

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

$( 'div.contactAperson input' ).focus(function() {
    $( this ).addClass( 'hadFocus' );
});
Run Code Online (Sandbox Code Playgroud)

感谢任何聪明 - 欢呼,-Alan

hwi*_*ers 5

您需要根据元素的当前状态运行处理程序 - 而不是绑定时的状态.您可能需要使用实时绑定.

尝试这样的事情:

$('div.contactAperson input:not(.hadFocus)').live('focus', function() {
    $(this).attr('value', '' );
});
Run Code Online (Sandbox Code Playgroud)


ozs*_*gal 4

$( 'div.contactAperson > :input' ).not( ':input.hadFocus' ).focus(function() {
    $(this).attr('value', '' );
});
Run Code Online (Sandbox Code Playgroud)

祝你好运