简体   繁体   English

使用 JavaScript 或 Jquery 将焦点设置为动态创建的 DIV

[英]Set focus to dynamically created DIV using JavaScript or Jquery

I have created我创造了

Generated code : 

<div id="showInfo_1" style="display: none">
--info 1
</div>

<div id="showInfo_2" style="display: none">
--info 2
</div>

jQuery : $('showInfo_2').focus();

this works fine in Chrome but not in IE.这在 Chrome 中运行良好,但在 IE 中不起作用。

could someone help me on this please.有人可以帮助我吗?

Note : I have posted answer as the way i tried... i just added return false in the onClick method like the following注意:我已经按照我尝试的方式发布了答案......我只是在onClick方法中添加了 return false ,如下所示

onclick="my method call; return false; " onclick="我的方法调用;返回假;"

Attempting to set focus to a hidden element causes an error in Internet Explorer.尝试将焦点设置到隐藏元素会导致 Internet Explorer 出错。 Take care to only use .focus() on elements that are visible.注意只在可见元素上使用 .focus() 。 To run an element's focus event handlers without setting focus to the element, use .triggerHandler( "focus" ) instead of .focus().要在不为元素设置焦点的情况下运行元素的焦点事件处理程序,请使用 .triggerHandler("focus") 而不是 .focus()。

from: https://api.jquery.com/focus/来自: https : //api.jquery.com/focus/

我刚刚在onclick添加了return false ,它开始按我预期的方式正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM