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

2023-12-13

由于某种原因,我无法将焦点设置在 popup.html 中的文本框上。到目前为止,这是我尝试过的:

弹出.html:

<input type="text" id="textbox" name="aName" value="" placeholder="blah" />

弹出.js:

//Attempt 1
$(function() {      
    $('#textbox').focus();
});

//Attempt 2
setTimeout(function() { $('#textbox').focus(); }, 1000);

我也尝试过不使用 javascript,只使用 autofocus 属性:

<input type="text" id="textbox" name="aName" value="" placeholder="blah" autofocus />

但这一切都不起作用......有任何想法吗?

Notes:

  • 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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法将焦点设置为 Chrome 扩展中的输入 的相关文章