删除 :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
Remove :focus with jquery
提问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 :focus
when 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
回答by Milind Anantwar
Try this:
尝试这个:
$(document).ready(function () {
$('#link').hover(function (){
$("input").blur();
}, function (){
//mouse leave
});
});
回答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");
});
});