所以我试图做一个简单的扩展,它打开一个新的选项卡中的网址。 问题是,我似乎无法弄清楚为什么我的函数(popup.js中的函数)在我提交表单时(按下回车键)没有运行。jQuery表单提交事件处理程序不会被调用(Chrome扩展)
这里是我的代码:
manifest.json的
{
"manifest_version": 2,
"name": "Chromerator",
"description": "Vim-like functionality added to chrome",
"version": "1.0",
"permissions": [
"tabs"
],
"background": {
"scripts": ["jquery-2.1.1.min.js"]
},
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
}
}
popup.html
<!doctype html>
<html>
<head>
<style type="text/css">
body {
min-width: 300px;
max-width: 300px;
}
#q {
width: 100%;
}
</style>
</head>
<script src="popup.js"></script>
<form class="f">
<input type="text" id="q" autofocus></input>
</form>
</body>
</html>
popup.js
$('.f').submit(function (event) {
var url = "http://" + &('#q').val();
chrome.tabs.create({ 'url': url });
event.preventDefault();
});
它工作,欢呼!虽然仍然有些奇怪,它运行时作为扩展运行,但是当我打开popup.html在铬,它不,为什么会这样?是关于权限? – 2014-11-16 14:07:48
检查错误控制台,很可能是在chrome.tabs.create({'url':url})上抛出一个错误;因为chrome对象只在作为扩展加载时才被定义。 – 2014-11-17 14:59:13