chrome扩展弹出文本区域焦点

chrome extension popup textarea focus

本文关键字:区域 焦点 文本 扩展 chrome      更新时间:2023-09-26

当我的chrome扩展弹出窗口的文本区域打开/在ondomready之后,我似乎无法对其调用focus()。

我在popup.js:中有类似的东西

$(document).ready(function () {
   console.log($("#moped-text"));
   $('textarea').focus();   
   $('.container').css("color", "red");
});

我在popup.html中这样引用它:

<html>
  <head>
    <link rel="stylesheet" href="css/content.css" type="text/css">
    <script type="text/javascript" src="js/lib/jquery.js"></script>
    <script type="text/javascript" src="js/popup.js"></script>
  </head>
 <body>
       <div class="container">
            <textarea name="newText"></textarea>
       </div>     
 </body>

css更改有效,focus()无效!

当我调试弹出窗口并键入$('extarea').focus();在控制台中,它可以工作。此外,在就绪回调中添加的事件也被成功绑定。

非常感谢您的帮助!

我仍然不知道,为什么焦点没有设置在第一位,但一个简单的超时就完成了任务:)

setTimeout(function() {
  $('#moped-text').focus();
}, 500);

Chrome 18的解决方案(在此处找到)

if(location.search !== "?foo")
{
  location.search = "?foo";
  throw new Error;  // load everything on the next page;
                    // stop execution on this page
}

对我有用!

您有一个类为"container"而非id的div。因此,请使用以下内容:

$('.container').css("color", "red");

我用它来聚焦。

我的元素具有"自动对焦"属性。加载表单时,具有"autofocus"属性的元素将获得焦点。

.指令("autofocus",["$timeout",function($timeout){返回{restrict:"A",链接:函数($scope,$element){$timeout(function(){$element[0].focus();});}}}])

您可以简单地使用<textarea name="newText" autofocus></textarea>