Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

javascript - Key press event is not working in Mozilla Firefox

Key press event is not working in Mozilla Firefox. I have create a table row dynamically with a text boix in it and that text box has a key press event too.

  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);

In google chrome the function additemifenter(this.id) is called. But in firefox that function is not getting executed. What is the alternate way to do this in firefox?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Maybe the semicolon at the end would help

el.setAttribute("onkeypress","additemifenter(this.id);");

but

why don't you use the standard event handling model:

el.onkeypress = function(event){
// functionality
};

or

el.addEventListener("keypress",function(event){ 
// functionality
},false);

to check the keycode you must use the code:

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

if(code == 13){

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...