Javascript 以编程方式移除焦点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7954205/
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-24 04:11:35  来源:igfitidea点击:

Remove focus programmatically?

javascriptjqueryfocusbind

提问by Jason

I have a jquery ui dialog with tabs loaded dynamically/JSON with content. Due to the complexity, I can't really post a fiddle or a relevant code (too much code).

我有一个 jquery ui 对话框,其中包含动态加载的选项卡/带有内容的 JSON。由于复杂性,我无法真正发布小提琴或相关代码(代码太多)。

What's happening is that when the dialog opens, you can tab through the elements within the dialog, up to the first tab. After that, you cannot tab through to anywhere else. The focus is locked on that tab, even if you click elsewhere, that focus is locked on that tab.

发生的事情是,当对话框打开时,您可以在对话框中的元素之间切换,直到第一个选项卡。之后,您将无法跳转到其他任何地方。焦点锁定在该选项卡上,即使您单击别处,该焦点也锁定在该选项卡上。

I am unable to locate the actual cause of this issue.

我无法找到此问题的实际原因。

So, how would I remove the focus programmatically?

那么,我将如何以编程方式删除焦点?

回答by Derek

This might help...

这可能有助于...

http://api.jquery.com/blur/

http://api.jquery.com/blur/

$('#tabName').blur();

回答by Mike Thomsen

Try triggering a blurevent on the field you want to lose focus.

尝试blur在您想要失去焦点的字段上触发一个事件。

回答by Vinnyq12

I'd put in a load of alerts to find where the JS is failing. E.g.

我会放入大量警报以查找 JS 失败的地方。例如

alert(1);
var a = 10;
alert(2);
var b = null;
alert(3);
a += 5;
alert(4);
b.hello();
alert(5);

Obviously in this example the last alert will be 4.

很明显,在这个例子中,最后一个警报将是 4。

It is not a nice approach but everyone has to do it at some point.

这不是一个好方法,但每个人都必须在某个时候这样做。