jQuery 如何从jQuery中的输入字段中删除焦点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26010791/
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
How to remove focus from input field in jQuery?
提问by user2571510
I am setting the focus on a certain input field when loading a page using the following line:
使用以下行加载页面时,我将焦点设置在某个输入字段上:
$('#myInputID').focus();
Is there a way that I can undo or remove this focus when hovering over a certain element? (The focus does not have to be reset after leaving this element.)
当悬停在某个元素上时,有没有办法撤消或移除这个焦点?(离开该元素后不必重新设置焦点。)
I couldn't find a function that is the opposite to the above in jQuery or would otherwise work here.
我在 jQuery 中找不到与上述相反的函数,否则在这里工作。
回答by Milind Anantwar
Use .blur().
使用.blur()。
The blur event is sent to an element when it loses focus. Originally, this event was only applicable to form elements, such as
<input>
. In recent browsers, the domain of the event has been extended to include all element types. An element can lose focus via keyboard commands, such as the Tab key, or by mouse clicks elsewhere on the page.
当元素失去焦点时,模糊事件被发送到元素。本来,这个事件只适用于表单元素,比如
<input>
. 在最近的浏览器中,事件的域已扩展为包括所有元素类型。元素可能会通过键盘命令(例如 Tab 键)或鼠标单击页面上的其他位置而失去焦点。
$("#myInputID").blur();
回答by Kuf
回答by Mujthaba Ibrahim
For all textbox :
对于所有文本框:
$(':text').blur()
回答by Vinayak
$(':text').attr("disabled", "disabled");
sets all textbox to disabled mode.
You can do in another way like giving each textbox id. By doing this code weight will be more and performance issue will be there.
$(':text').attr("disabled", "disabled");
将所有文本框设置为禁用模式。你可以用另一种方式来做,比如给每个文本框 id。通过这样做,代码重量会更多,并且会出现性能问题。
So better have $(':text').attr("disabled", "disabled");
approach.
所以最好有$(':text').attr("disabled", "disabled");
办法。
回答by Jeffz
If you have readonly
attribute, blur by itself would not work. Contraption below should do the job.
如果你有readonly
属性,模糊本身是行不通的。下面的装置应该可以完成这项工作。
$('#myInputID').removeAttr('readonly').trigger('blur').attr('readonly','readonly');