javascript - Chrome 应用程序中的按钮不起作用

标签 javascript html google-chrome google-chrome-app

好吧,我有一个打包的应用程序,这个应用程序包含index.html和其他页面.. index.html 包含链接到其他页面的按钮..

问题是按钮不起作用!当我点击按钮时......什么也没有发生。

这是主要文件:

{
  "name": "TextTools",
  "description": "Simple tools for Text.",
  "version": "0.0.0.1",
  "manifest_version": 2,
  "icons": {
    "128": "logo.png"
  },

"app": {
    "launch": {
      "local_path": "index.html"
    }
  }
  }

这是 Index.html 正文标记:

<body>

<center><div id="container">

<div><input id="btn1" type="button" value="Letters Counter" class="btn1" 
onclick="location.href='LettersCounter/LengthCalculator.html';"></div>


<div><input id="btn2" type="button" value="Case Converter"  class="btn2"
onclick="location.href='CaseConverter/CaseConverter.html';"/></input></div>



<div><input type="button" value="Text Capitalizer" class="btn3" 
onclick="location.href='CapitalizeText/CapitalizeText.html';"  /></div>


<div><input type="button" value="Words Counter" class="btn4"
onclick="location.href='WordsCounter/WordsCounter.html';" /></div>


<div><input type="button" value="QR-Generator" class="btn5"
onclick="location.href='QRcode/index.html';"  /></div>


</div></center>


<div id="foot" >

<p style="color:#B8B8B8; margin-left:10px; margin:10px; font-family: Gill Sans, sans-serif; font-size : 14px; "></p>
</div>

</body>

我知道这很简单,但我是新人:)

最佳答案

问题

您正在使用内联javascript Content Security Policy for Chrome Apps 不允许使用该js。 :

The content security policy for Chrome Apps restricts you from doing the following:

  • You can’t use inline scripting in your Chrome App pages. The restriction bans both blocks and event handlers (<button onclick="...">).

[...]

<小时/>

解决方案

使用外部.js注册监听器的文件。例如:

/* Instead of... */
<input id="btn1" type="button" onclick="..." ... />

/* ...change HTML to... */
<input id="btn1" type="button" ... />
...
<script type="text/javascript" src="myScript.js"></script>

/* ...and in `myScript.js` add... */
document.getElenentById("btn1").addEventListener("click", function() {
    ...
});

关于javascript - Chrome 应用程序中的按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20012821/

相关文章:

javascript - 如何在调整窗口大小时禁用/重新启用 Javascript?

jquery - Bootstrap 3 : Show the error message with fixed Grid Layout

javascript - Chrome JavaScript 调试器 : how to enter value and see it?

javascript - JavaScript 中的此属性范围

javascript - 在 Polymer 组件的生命周期中,何时会因声明指定的值而触发更改的观察者?

javascript - Facebook 类型弹出下拉列表重新创建(附图)

javascript - Ajax 从不正确的 URL 错误地成功获取

javascript - Html 节点内的信息不可见

javascript - 使用 JavaScript 控制台修改脚本的结果

html - 宽度为 100% 的 td 在 div 内,位置为绝对