我写了一些代码来检查用户是否在Google上启动搜索(点击“搜索”按钮或输入Enter)。
我的代码工作正常(这是Firefox扩展),但我不能因为一个不明原因而无法获取密钥的代码。
我的代码:
window.addEventListener("submit", function() { myExtension_with_click.init(); }, false);
window.addEventListener("keypress", function() { myExtension_with_keypress.init(); }, false);
var myExtension_with_click = {
init: function() {
alert("This works");
}
}
var myExtension_with_keypress = {
init: function() {
alert("This works")
if (window.event.keyCode == 13) {
// This doesn't work
alert("This doesn't work");
}
}
}
答案 0 :(得分:1)
尝试这样做有效
<html>
<head>
<script>
window.addEventListener("submit", function() { myExtension_with_click.init(); }, false);
window.addEventListener("keypress", function(event) { myExtension_with_keypress.init(event); }, false);
var myExtension_with_click = {
init: function() {
alert("This works");
}
}
var myExtension_with_keypress = {
init: function(event) {
//alert("This works")
if (event.keyCode == 13) {
// This doesn't work
alert("This doesn't work");
}
}
}
</script>
</head>
<body>
<input />
</body>
答案 1 :(得分:0)
一个简单的解决方案是在事件监听器中使用事件“change”!