cet*_*int 3 google-chrome-extension
的manifest.json
{
"name": "Summer",
"version": "1.0",
"manifest_version": 2,
"description": "This is an addition extension",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
}
}
Run Code Online (Sandbox Code Playgroud)
popup.html
<!doctype html>
<html>
<head>
<title>Getting Started Extension's Popup</title>
<!-- JavaScript and HTML must be in separate files for security. -->
<script src="popup.js"></script>
</head>
<body>
<form name="form">
<div id="sayi1">Say? 1 : <input type = "text" name="deger1"></div>
<div id="sayi2">Say? 2 : <input type = "text" name="deger2"></div>
<div id="sonuc">Sonuç : <input type = "text" name="cevap"></div>
<div id="button"><input type="button" value="Hesapla" onclick="hesaplama()" /></div>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
popup.js
function hesaplama()
{
var sayi1 = window.document.form.deger1.value;
var sayi2 = window.document.form.deger2.value;
var toplam = parseFloat(sayi1) + parseFloat(sayi2) ;
window.document.form.cevap.value = toplam;
}
Run Code Online (Sandbox Code Playgroud)
当我加载此扩展时,我可以正常看到.但是当我填充deger1和deger2文本框并单击按钮时,函数不起作用,在sonuc文本框(结果文本框)中为null.我该如何解决?我是创建Chrome扩展程序的新手.谢谢你的帮助.
你已经进入manifest_version : 2了你的清单,所以请仔细阅读它引入的变化....
http://code.google.com/chrome/extensions/manifestVersion.html
你进入控制台Refused to execute inline event handler because of Content-Security-Policy,因为onclick事件处理程序在你的html(<input type="button" value="Hesapla" onclick="hesaplama()" />)中.对于清单版本2,这是不允许的,您需要从js代码而不是在html中附加事件列表器.
这是你的代码的工作版本....
popup.html
<!doctype html>
<html>
<head>
<title>Getting Started Extension's Popup</title>
<!-- JavaScript and HTML must be in separate files for security. -->
<script src="popup.js"></script>
</head>
<body>
<form name="form">
<div id="sayi1">Say? 1 : <input type = "text" name="deger1"></div>
<div id="sayi2">Say? 2 : <input type = "text" name="deger2"></div>
<div id="sonuc">Sonuç : <input type = "text" name="cevap"></div>
<div id="button"><input type="button" value="Hesapla" /></div>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
popup.js
function hesaplama()
{
var say?1 = window.document.form.deger1.value;
var say?2 = window.document.form.deger2.value;
var toplam = (parseFloat(say?1) + parseFloat(say?2)) ;
window.document.form.cevap.value = toplam;
}
window.onload = function(){
document.querySelector('input[value="Hesapla"]').onclick=hesaplama;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2077 次 |
| 最近记录: |