jquery - 无法将焦点设置为 Chrome 扩展中的输入

标签 jquery google-chrome-extension

出于某种原因,我无法将焦点设置在 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>

enter image description here

关于jquery - 无法将焦点设置为 Chrome 扩展中的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11347149/

相关文章:

javascript - 如果数字为 1 或 0,则显示不同的图标 - ASP.NET C# MVC JQuery

google-chrome - 文件更改时自动刷新浏览器?

google-chrome-extension - 谷歌浏览器扩展程序获取新标签 ID

google-chrome-extension - Microsoft Edge 扩展工具栏操作按钮图标不可见

javascript - 处理切换按钮上的焦点

jquery - 如何激活单击的选项卡和非事件的默认选项?

JQuery $this 选择器 - 如何使其工作

iframe 中的 Javascript 日期选择器错误

javascript - 如何将调试器附加到设备? Chrome 扩展

javascript - 如何使 Facebook 的 Chrome 扩展重复执行?