jQuery:如何在没有特定类的情况下查找元素 [英] jQuery: How to find elements *without* a certain class

查看:90
本文介绍了jQuery:如何在没有特定类的情况下查找元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这为什么会失败...

Why does this fail...

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

...这意味着要嗅出没有 获得了.hadFocus类的输入,然后当该子集中的一个子集获得焦点时,应将该值转换为null.

...it's meant to sniff out input's that have not got the class .hadFocus and then when one of that subset receives focus it should zap the value to null.

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

Right now, input values are always getting zapped -- the test .not( 'input.hadFocus' ) is failing to stop execution.

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

Btw, preceding the above code is the following code, which is working fine:

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

感谢您的聪明-欢呼声-艾伦

Thanks for any cleverness - cheers, -Alan

推荐答案

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

You need the handler to run based on the current state of the element - not the state when it was bound. You probably need to use a live binding.

尝试这样的事情:

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

这篇关于jQuery:如何在没有特定类的情况下查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆