首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在chrome扩展中正确链接JavaScript

在chrome扩展中正确链接JavaScript
EN

Stack Overflow用户
提问于 2016-04-27 00:40:50
回答 1查看 125关注 0票数 0

因此,我有一个铬扩展,但JavaScript没有正确加载。我试图链接一个文件,但不幸地失败了。对于如何使按钮在单击时运行函数,有什么想法吗?标记在这种情况下不起作用,按钮什么也不做,但是初始的JavaScript确实运行.这是我的密码

Manifest.json(我认为它是正确的):

代码语言:javascript
运行
复制
{
"manifest_version": 2,

"name": "Getting started example",
"description": "This extension shows a Google Image search result for the current page",
"version": "1.0",

"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"activeTab",
"https://ajax.googleapis.com/"
 ]

  }

Popup.html( JavaScript中的函数不链接到此):

代码语言:javascript
运行
复制
<body style = "margin:10px">

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src="popup.js"></script>
<style>
//Fancy styles
large{font-size:30px}
span{}
.input-group{width:400px}
</style>
<!-- title -->
<large>
SpeedyURLs
</large>
<hr/>
<!-- Code that won't link with JavaScript -->
<div id = "stuff">

</div>
<div class = "input-group" id = "content"><input class = "form-control" placeholder = "Enter a URL here. Ex: https://www.google.com"/><span class="input-group-addon btn btn-default" style = "font-size:20px;width:50px;color:black" id="basic-addon1" onclick = "addNew()">+        </span></div>
<button onclick = 'document.write("HELLO")'>YAS</button>
</body>

Popup.js(不链接的函数):

代码语言:javascript
运行
复制
function open(url){
window.open(url)}
function addNew(){
document.getElementById("stuff").innerHTML = document.getElementById("stuff").innerHTML+'<div class = "input-group" id = "content"><input class = "form-control" placeholder = "Enter a URL here. Ex: https://www.google.com"/><span  style = "font-size:20px;width:50px;color:black" class="input-group-addon btn btn-danger" id="basic-addon1">x</span></div>'}

Icon.png(默认图像中没有代码,工作正常)

因此,JS中的函数不会链接到HTML。有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-27 01:00:16

默认情况下,将不会执行内联JavaScript。

你可以

  1. 放松默认策略。有关更多详细信息,请参阅内联脚本
  2. 删除onclick中的popup.html并将该逻辑移动到您的popup.js中,代码将类似于(假设您的按钮id是buttonId0) document.getElementById("buttonId0").addEventListener("click",函数(){ //您的逻辑在这里},false);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36878293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档