jQuery模糊()不工作? [英] jQuery blur() not working?

查看:76
本文介绍了jQuery模糊()不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

完全被这里难住了。
尝试一些非常简单的东西,但它不起作用:

Totally stumped here. Trying something pretty easy, but it's not working:

$("input.input1, textarea.input1").focus(function(){
    $(this).addClass("input2").removeClass("input1");
});
$("input.input2, textarea.input2").blur(function(){
    $(this).addClass("input1").removeClass("input2");
});

基本上模糊不会触发。
我点击输入框,框就会改变。
我点击框即可,没有任何反应。
任何帮助都会很棒。

Basically the blur isn't firing. I click on the input box and the box changes. I click away from the box and nothing happens. Any help with this would be awesome.

推荐答案

你需要使用委托的处理程序,因为输入不是在应用处理程序时具有类 input2

You need to use a delegated handler as the input doesn't have class input2 at the time the handler is applied.

$(document).on('focus','input.input1, textarea.input1', function() {
    $(this).addClass('input2').removeClass('input1');
});
$(document).on('blur','input.input2, textarea.input2', function() {
    $(this).addClass('input1').removeClass('input2');
});

然而,可能有更好的方法。我建议使用第三个类来标记需要切换类的输入,然后在事件发生时切换类。

There are probably better ways to do this, however. I'd suggest using a third class to mark the inputs that need the class toggled, then just toggle the classes when the event occurs.

$('.needsToggle').on('focus blur', function() {
     $(this).toggleClass('input1 input2');
});

如果他们总是有类 input1 来启动,您可以使用它来代替 needsToggle

If they always have class input1 to start, you could use that instead of needsToggle.

这篇关于jQuery模糊()不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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