chrome扩展popup textarea焦点

似乎我无法在我的chrome扩展弹出窗口的textarea上调用focus()。

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

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

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

        

css-change工作,focus()没有!

当我调试弹出窗口并输入$(’textarea’)。focus(); 在控制台中,它的工作原理。 此外,在ready-callback中添加的事件也会成功绑定。

任何帮助高度赞赏!

我仍然不知道,为什么焦点没有设置在第一位,但一个简单的超时工作:)

 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 } 

适合我!

你有div“class”而不是id。 所以请改用:

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

我用它来集中注意力。

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

.directive(’autofocus’,[‘$ timeout’,function($ timeout){return {restrict:’A’,link:function($ scope,$ element){$ timeout(function(){$ element [0] .focus();});}}}])

您只需使用