经过多次尝试,我发现无论我做什么:
function foo(){
$("input").focus();
}
<body onload="foo();">
<input type="text" autofocus tabindex="1">
我什至使用了一个将焦点设置在它上面的隐藏按钮,然后我以编程方式单击它,它甚至不会在弹出页面中将焦点放在它上面,这让我发疯。我检查了很多次代码,我找不到一个错误。所以我真的很想知道是否有实际可行的方法来实现这一目标,因为这看起来就像一个简单的问题。
最佳答案
这在 Chrome 版本 21 上不再是问题。其他最新版本的 Chrome 可能也有这个 issue resolved .
例子
list .json
{
"name": "Stackoverflow Popup Form Focus",
"manifest_version": 2,
"version": "0.1",
"description": "Simple popup with form with form element focused",
"browser_action": {
"default_popup": "popup.html"
}
}
popup.html
<html>
<head>
<style type="text/css" media="screen">
body { width: 300px; height: 100px; text-align: center; }
</style>
</head>
<body>
<p>This input should focus when this opens</p>
Party Input: <input id='party-input'/>
<script src="popup.js"></script>
</body>
</html>
弹窗.js
document.getElementById('party-input').focus();
运行示例截图
在浏览器窗口打开到 exampley.com 的情况下单击扩展按钮
示例文件 http://mikegrace.s3.amazonaws.com/stackoverflow/popup-form-focus.zip
关于javascript - 弹出页面中的自动对焦(甚至对焦)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9266916/