按键事件在 Mozilla Firefox 中不起作用。 我动态创建了一个表行,其中包含一个文本框,并且该文本框也有一个按键事件。
var el = document.createElement('input');
el.type = 'text';
el.name = 'suggest2';
el.setAttribute("id",str2);
el.setAttribute("onkeypress","additemifenter(this.id)");
cell2.appendChild(el);
row.appendChild(cell2);
在 google chrome 中,调用函数 additemifenter(this.id)。但在 Firefox 中该函数没有被执行。在 Firefox 中执行此操作的替代方法是什么?
最佳答案
也许末尾的分号会有帮助
el.setAttribute("onkeypress","additemifenter(this.id);");
但是
为什么不使用标准事件处理模型:
el.onkeypress = function(event){
// functionality
};
或
el.addEventListener("keypress",function(event){
// functionality
},false);
要检查 key 代码,您必须使用以下代码:
var code = (event.keyCode) ? event.keyCode : event.which;
if(code == 13){
}
关于javascript - 按键事件在 Mozilla Firefox 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6235689/