出于某种原因,我无法将注意力集中在我的popup.html中的texbox上。这是我到目前为止所尝试的内容:
popup.html:
<input type="text" id="textbox" name="aName" value="" placeholder="blah" />
popup.js:
//Attempt 1
$(function() {
$('#textbox').focus();
});
//Attempt 2
setTimeout(function() { $('#textbox').focus(); }, 1000);
我也试过没有javascript,只使用自动对焦属性:
<input type="text" id="textbox" name="aName" value="" placeholder="blah" autofocus />
But none of this worked...有什么想法吗?
注意:
答案 0 :(得分:3)
此代码与我合作,尝试一下,这是一种解决方法
<!DOCTYPE >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sample Extens</title>
</head>
<body style="padding: 0px; margin: 0px;" >
<script type="text/javascript" language="javascript">
if (location.search !== "?foo") {
location.search = "?foo";
throw new Error; // load everything on the next page;
// stop execution on this page
}
</script>
<div id="Def">
<input type="text" id="textbox" name="aName" value="" placeholder="blah" />
<script type="text/javascript" language="javascript">
document.getElementById("textbox").focus();
</script>
</div>
</body>
</html>
答案 1 :(得分:3)
我有同样的问题。我相信我能够通过在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
<!doctype html>
<html>
<head>
</head>
<body>
<a href="#">Link</a>
<input type="text" id="foo" tabindex="1" />
</body>
</html>
没有tabindex="1"
,焦点最初是在链接上。使用tabindex="1"
时,焦点在于输入元素
答案 2 :(得分:2)
最后我用过的是:
popup.html:
<input type="text" id="textbox" name="aName" value="" placeholder="blah" autofocus />
popup.js:
$(function() {
if (location.search != "?focusHack") location.search = "?focusHack";
});
感谢 Tarek El-Mallah 和 PAEz !!!
答案 3 :(得分:1)
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
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="popup.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sample Extens</title>
</head>
<body style="padding: 0px; margin: 0px;" >
<div id="Def">
<input type="text" id="textbox" name="aName" value="" placeholder="blah" />
</div>
</body>
</html>
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;
答案 4 :(得分:0)
试试这个:
autofocus="autofocus"
您也可以尝试这样做:
setTimeout(
function() {
if(location.search !== "?aName") {
location.search = "?aName";
throw new Error;
}
}, 1000);