出于某种原因,我无法将焦点设置在 popup.html 中的文本框上。这是我到目前为止所尝试过的:
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...有什么想法吗?
注释:
- popup.js 正在被调用,如果我输入 console.log() 我会得到输出
- 弹出窗口是由多功能栏旁边的图标(default_icon)触发的
最佳答案
这段代码对我有用,试试吧,这是一个解决方法
<!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>
关于jquery - 无法将焦点设置为 Chrome 扩展中的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11347149/