我想将图像用作按钮(setting.png),然后在新选项卡中单击打开URL。

我的popup.html:

    <html>
  <head>

    <style>
    </style>
  </head>
  <body>
    <a href = "http://google.com" id="link"><img src="setting.png" width="30px" height="auto"></a>
    <script src ="javascript.js"></script>
  </body>
</html>


manifest.json:

{
  "manifest_version": 2,

  "name": "",
  "options_page": "options.html",
  "background": {
        "scripts": [
            "background.js"
        ]
    },
  "description": "",
  "version": "1.5",
  "offline_enabled": true,
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "icons": { "16": "icon16.png",
              "32": "icon32.png",
           "48": "icon48.png",
           "64": "icon64.png",
          "128": "icon128.png" },

  "permissions": [
    "activeTab",
    "https://ajax.googleapis.com/",
    "storage",
    "tabs"
  ]
}


javascipt.js:

window.addEventListener('click',function(e){
  if(e.target.href!==undefined){
    chrome.tabs.create({url:e.target.href})
  }
})


因此,当我单击图像时,我想在新选项卡中打开URL。谢谢!

最佳答案

Chrome会在图像的event.target中设置最里面被点击的元素。
不需要click处理程序,只需将target="_blank"属性用于链接:




<a href="http://google.com" id="link" target="_blank">


附言如果出现问题,请始终使用debugger来查看实际发生的情况(在click handler中设置断点,单击链接,检查参数,等等)。

10-07 14:43