无法将焦点设置为在chrome扩展中输入
出于某种原因,我无法将注意力集中在我的popup.html中的texbox上。 这是我到目前为止所尝试的内容:
popup.html:
popup.js:
//Attempt 1 $(function() { $('#textbox').focus(); }); //Attempt 2 setTimeout(function() { $('#textbox').focus(); }, 1000);
我也试过没有javascript,只使用autofocus属性:
但这一切都没有奏效……有什么想法吗?
笔记:
- 正在调用popup.js,如果我把console.log()我得到输出
- 弹出窗口由我们旁边的omnibar(default_icon)旁边的图标触发
此代码与我合作,尝试它,这是一个解决方法
Sample Extens
我有同样的问题。 我相信我能够通过在输入上设置明确的tabindex来实现它,如tabindex=1
请尝试一下,让我知道它是否有效。
更新
我有一个非常简单的例子适合我。 我在Linux上使用Chrome 19。
manifest.js
{ "name": "Auto 'focus'", "version": "1.0", "manifest_version": 2, "description": "An extension to test setting focus", "browser_action": { "default_icon": "icon.png", "default_popup": "popup.html" } }
popup.html
Link
没有tabindex="1"
,焦点最初是在Link上。 使用tabindex="1"
,焦点位于输入元素上
最后我用过的是:
popup.html:
popup.js:
$(function() { if (location.search != "?focusHack") location.search = "?focusHack"; });
谢谢Tarek El-Mallah和PAEz !
Tarek El-Mallah重新加载弹出窗口的方法确实有效,它只是不适合你使用manifest_version:2和内联脚本是不允许的…
http://code.google.com/chrome/extensions/manifestVersion.html
此外,这是一个已知问题….
http://code.google.com/p/chromium/issues/detail?id=111660
以下是适用于manifest_version的版本:2 …..
的manifest.json
{ "name": "Browser Action PopUp focus/tab test", "version": "1.0", "description": "A test to show that on opening a popup you cant set focus and the tab index is not honored on the first select. See, http://stackoverflow.com/questions/9070727/tab-key-not-working-in-popup-in-chrome-extension.", "browser_action": { "default_title": "Browser Action PopUp focus/tab test.", "default_icon": "icon.png", "default_popup": "popup.html" }, "manifest_version" :2 }
popup.html
Sample Extens
popup.js
if (location.search !== "?foo") { location.search = "?foo"; throw new Error; // load everything on the next page; // stop execution on this page } function onLoad() { document.getElementById("textbox").focus(); } window.onload = onLoad;
试试这个:
autofocus="autofocus"
您也可以尝试这样做:
setTimeout( function() { if(location.search !== "?aName") { location.search = "?aName"; throw new Error; } }, 1000);