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

时间:2010-04-26 17:06:57

标签: jquery-selectors

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

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

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

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

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

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

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

2 个答案:

答案 0 :(得分:5)

您需要根据元素的当前状态运行处理程序 - 不是受约束的国家。您可能需要使用live绑定。

尝试这样的事情:

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

答案 1 :(得分:4)

$( 'div.contactAperson > :input' ).not( ':input.hadFocus' ).focus(function() {
    $(this).attr('value', '' );
});
祝你好运