3

出于某种原因,我无法将焦点放在 popup.html 中的 texbox 上。这是我到目前为止所尝试的:

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,只使用 autofocus 属性:

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

但这些都不起作用......有什么想法吗?

笔记:

  • popup.js 被调用,如果我把 console.log() 我得到输出
  • 弹出窗口由我们在omnibar(default_icon)旁边的图标触发
4

5 回答 5

4

我有同样的问题。我相信我可以通过在输入上设置一个明确的 tabindex 来让它工作,比如tabindex=1

请试一试,让我知道它是否有效。

更新

我有一个非常简单的例子对我有用。我在 Linux 上使用 Chrome 19。

manifest.js

{
"name": "Auto 'focus'",
    "version": "1.0",
    "manifest_version": 2,
    "description": "An extension to test setting focus",
    "browser_action": {
        "default_icon": "icon.png",
        "default_popup": "popup.html"
    }
}

popup.html

<!doctype html>
<html>
  <head>
  </head>
  <body>
    <a href="#">Link</a>
    <input type="text" id="foo" tabindex="1" />
  </body>
</html>

没有tabindex="1"焦点最初是在链接上。tabindex="1"焦点在输入元素上

于 2012-07-05T21:39:51.007 回答
3

最后我用的是这个:

popup.html:

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

popup.js:

$(function() {              
    if (location.search != "?focusHack") location.search = "?focusHack";
});

感谢Tarek El-MallahPAEz!!!

于 2012-07-09T18:10:24.367 回答
3

这段代码适用于我,试试吧,这是一种解决方法

<!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>

在此处输入图像描述

于 2012-07-05T16:07:14.317 回答
1

Tarek El-Mallah 重新加载弹出窗口的方法确实有效,但它对您不起作用,因为您使用 manifest_version : 2 并且不允许使用内联脚本...
http://code.google.com/chrome/extensions/manifestVersion。 html
另外,这是一个已知问题....
http://code.google.com/p/chromium/issues/detail?id=111660
以下是适用于 manifest_version 的版本:2.....

清单.json

{
  "name": "Browser Action PopUp focus/tab test",
  "version": "1.0",
  "description": "A test to show that on opening a popup you cant set focus and the tab index is not honored on the first select.  See, http://stackoverflow.com/questions/9070727/tab-key-not-working-in-popup-in-chrome-extension.",
  "browser_action": {
      "default_title": "Browser Action PopUp focus/tab test.",
      "default_icon": "icon.png",
      "default_popup": "popup.html"
  },
  "manifest_version" :2
}

popup.html

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="popup.js"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Sample Extens</title>
</head>
<body style="padding: 0px; margin: 0px;" >
    <div id="Def">
        <input type="text" id="textbox" name="aName" value="" placeholder="blah" />
    </div>
</body>
</html>

popup.js

if (location.search !== "?foo") {
    location.search = "?foo";
    throw new Error; // load everything on the next page;
    // stop execution on this page
}

function onLoad() {
    document.getElementById("textbox").focus();
}

window.onload = onLoad;
于 2012-07-09T13:22:12.250 回答
0

Try this:

 autofocus="autofocus"

You can also try this instead:

setTimeout(
    function() { 
        if(location.search !== "?aName") {
           location.search = "?aName";
           throw new Error; 
        }
    }, 1000);
于 2012-07-05T15:07:59.480 回答