按键事件在Mozilla Firefox中无效

Var*_*ada 2 javascript mozilla keypress

按键事件在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);
Run Code Online (Sandbox Code Playgroud)

在谷歌浏览器中,调用additemifenter(this.id)函数.但在Firefox中,该功能没有被执行.在Firefox中执行此操作的替代方法是什么?

Hea*_*ota 6

也许最后的分号会有所帮助

el.setAttribute("onkeypress","additemifenter(this.id);");
Run Code Online (Sandbox Code Playgroud)

为什么不使用标准事件处理模型:

el.onkeypress = function(event){
// functionality
};
Run Code Online (Sandbox Code Playgroud)

要么

el.addEventListener("keypress",function(event){ 
// functionality
},false);
Run Code Online (Sandbox Code Playgroud)

要检查密钥代码,您必须使用代码:

var code = (event.keyCode) ? event.keyCode : event.which;

if(code == 13){

}
Run Code Online (Sandbox Code Playgroud)