2012-07-05 57 views
2

出于某种原因,我无法将焦点设置在我的popup.html中的texbox上。以下是我试过到目前为止:无法将焦点设置为Chrome扩展中的输入

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()我得到的输出
  • 弹出窗口是由我们旁边omnibar的图标触发(default_icon)

回答

2

最后我所用是这样的:

popup.html:

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

popup.js:

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

感谢塔里克埃尔 - MallahPAEz !!!

+2

啊,好的...那是1小时,我不会回来...: - \ – drphrozen 2012-08-03 21:05:21

0

试试这个:

autofocus="autofocus" 

你也可以试试这个:

setTimeout(
    function() { 
     if(location.search !== "?aName") { 
      location.search = "?aName"; 
      throw new Error; 
     } 
    }, 1000); 
+0

他们不工作:( – epzee 2012-07-05 15:29:55

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> 

enter image description here

+0

如果我把js代码放在html中,我得到这个错误信息:“拒绝执行内联脚本,因为Content-Security-Policy”如果我把它放在popup.js中?不行......你要为在清单的任何特殊权限 – epzee 2012-07-05 17:26:13

+1

这是我的清单 { “名”:“测试”, “版本”:“1.1”, “说明”:“ testttt”, “browser_action”:{ “default_title”: “testttt testttt”, “default_icon”: “128.ico”, “弹出”: “testttt.html” }, “图标”:{ “16”:“16.ico”, “48”:“48.ico”, “128”:“128.ico” }, “permissions”:[ “http://*.testttt .com/*“ ] } – 2012-07-05 17:57:27

+2

如果您的清单是版本2,则无法在您的html中执行脚本。它必须从另一个文件中获取。 Tarek的例子是明确的版本1.1 – jcbwlkr 2012-07-09 15:41:41

3

我有同样的问题。我相信我能够通过在输入上设置一个明确的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"焦点在输入元素上

+0

谢谢,但它没有工作:( – epzee 2012-07-06 04:32:24

+0

嗯,我知道我有这个问题,并以某种方式修复它,我会看看我什么时候回到星期一工作 – jcbwlkr 2012-07-07 02:27:39

+0

我更新了我的答案,一个简单的例子我不知道为什么它在你的扩展中不起作用。谢谢。 – jcbwlkr 2012-07-09 15:51:29

1

重装弹出的塔里克埃尔 - Mallah的方法工作,它作为您使用manifest_version只是没有工作:2个内嵌脚本的arent允许...
http://code.google.com/chrome/extensions/manifestVersion.html
此外,这是一个已知的问题....
http://code.google.com/p/chromium/issues/detail?id=111660
以下是对于manifest_version工作的版本:2 .....

manifest.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> 

弹出.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;