【问题标题】:Set focus to dynamically created DIV using JavaScript or jQuery使用 JavaScript 或 jQuery 将焦点设置为动态创建的 DIV
【发布时间】:2018-11-19 17:33:04
【问题描述】:

我已经创建了

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();

这在 Chrome 中可以正常工作,但在 IE 中不行。

接下来我可以尝试什么?

【问题讨论】:

  • 你需要$('#showInfo_2').focus();。注意#。
  • ^^^ 鉴于上述评论,这目前在任何浏览器中都不适用。
  • 抱歉拼写错误。是的,添加了“#”,但在 IE 11 中仍然无法使用,但在 chrome 中可以正常使用
  • focus doesn't work in IE的可能重复

标签: javascript jquery focus


【解决方案1】:

尝试将焦点设置到隐藏元素会导致错误 IE浏览器。注意只在元素上使用 .focus() 可见的。在不设置的情况下运行元素的焦点事件处理程序 聚焦到元素,使用 .triggerHandler("focus") 而不是 .focus()。

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-08
      • 1970-01-01
      • 2012-07-29
      • 1970-01-01
      相关资源
      最近更新 更多