jQuery .focus实际上并不关注新创建的元素 [英] jQuery .focus doesn’t actually focus a newly created element

查看:88
本文介绍了jQuery .focus实际上并不关注新创建的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有此标记:

<div id="content">
  <div id="firstP"><p>First paragraph</p></div>
  <div id="secondP"><p>Second paragraph</p></div>
  <div id="thirdP"><p>Third paragraph</p></div>
  <div id="fourthP"><p>Fourth paragraph</p></div>
</div>

我想用jQuery添加一个新的<div>并专注于这个新元素. .focus不执行任何操作.

I want to add a new <div> with jQuery and focus on this new element. .focus does not do anything.

function addParagraph() {
  var html = "<div id=\"newP\"><p>New paragraph</p></div>";

  $("#content").append(html);
  $("#newP").focus();    
}

知道为什么吗?

推荐答案

我认为主要答案不正确.如果您为DIV和P标签指定了tabindex属性,则它们可以得到关注.即

I think the main answer is incorrect. DIV and P tags can receive focus providing you specify a tabindex property for them. i.e.

<div class="someclass" tabindex="100">

指定了tabindex后,您可以使用Tab键浏览这些元素,也可以使用 .focus()转移焦点.

Once the tabindex is specified you can either tab to these elements or shift focus with .focus() .

在这里使用scrollTo插件似乎有点过分.

Using a scrollTo plugin seems like a bit of an overkill here.

这篇关于jQuery .focus实际上并不关注新创建的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆