2012-07-05 131 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;