删除 :focus 与 jquery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21278995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:31:09  来源:igfitidea点击:

Remove :focus with jquery

jqueryhtmlcss

提问by MajAfy

I have an input field. When a user focuses on this field, the background of this field will change.

我有一个输入字段。当用户关注该字段时,该字段的背景会发生变化。

I have a link on my page, too. I want to remove :focuswhen the mouse hovers on this link and the user is on that input field.

我的页面上也有链接。:focus当鼠标悬停在此链接上并且用户在该输入字段上时,我想删除。

I use removeClass(':focus')but this code does not work!

我使用removeClass(':focus')但此代码不起作用!

HTML:

HTML:

<form>
    <input type="text" name="email" />
</form>
<a href="#" id="link">Sample Link</a>

Javascript:

Javascript:

$(document).ready(function () {
    $('#link').hover(function (){
        $('form input[name=email]').removeClass(':focus');        
    }, function (){
        $('form input[name=email]').addClass(':focus');        
    });
});

CSS:

CSS:

form input[name=email]:focus {
    background-color: yellow;    
}

Here is a fiddlefor the above

这是上面的小提琴

What should I do?

我该怎么办?

回答by CodingIntrigue

You need to use the in-built blurand focusmethods:

您需要使用内置的blurfocus方法:

$(document).ready(function () {
    $('#link').hover(function (){
        $('form input[name=email]').blur();
    }, function (){
        $('form input[name=email]').focus();
    });
});

回答by Milind Anantwar

Try this:

尝试这个:

$(document).ready(function () {
$('#link').hover(function (){
     $("input").blur(); 
 }, function (){
      //mouse leave  
});
});

Working FIddle

工作小提琴

回答by Somnath Kharat

Try this:

尝试这个:

$(document).ready(function () {
    $('#link').hover(function (){
        $('form input[name=email]').blur();  
        $('form input[name=email]').css("background-color","");     
    }, function (){
        $('form input[name=email]').focus(); 
        $('form input[name=email]').css("background-color"," yellow");       
    });
});