javascript - 弹出页面中的自动对焦(甚至对焦)?

标签 javascript google-chrome google-chrome-extension

经过多次尝试,我发现无论我做什么:

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 的情况下单击扩展按钮

enter image description here

示例文件 http://mikegrace.s3.amazonaws.com/stackoverflow/popup-form-focus.zip

关于javascript - 弹出页面中的自动对焦(甚至对焦)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9266916/

相关文章:

javascript - Angular 自定义验证

javascript - 在 chrome.storage 中存储一个 base64 字符串

javascript - 在 Chrome 上上传之前显示 pdf 的预览

javascript - 无法在 Chrome 扩展程序中提交表单

javascript - 我如何在 Meteor 的 Mongo 查询中使用变量作为字段名称?

javascript - 如何使用 AJAX 从另一台服务器获取时间?

google-chrome - chrome/safari 中的 css 透视差异

html - 创建 Chrome 扩展以隐藏 DIV 显示 :none; on a specific page

javascript - chrome.webNavigation.onCompleted 和 chrome.tabs.onUpdated.addListener 与 'complete' 有什么区别

css - 强制 iframe 中的元素显示在页面的其余部分