javascript 我想在 html 中使用 addeventlistener 而不是 onkeyup
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20137153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:50:25 来源:igfitidea点击:
I want to use addeventlistener instead of onkeyup in html
提问by Suneesh
<center><h14>for typing help<a href="help.html"> click here</a></h14></center>
<td valign="top"><center>
//in this line I want to use addEvent instead of onkeyup
<textarea id="ta_in" rows="7" cols="42" onkeyup="get_ml()"></textarea>
<br>
<textarea id="ta_out" rows=7" cols="42"></textarea></center>
</td>
回答by Brandon Boone
function get_ml(){
alert('in');
}
var el = document.getElementById('ta_in');
if (typeof el.addEventListener != "undefined") {
el.addEventListener("keyup", function(evt) {
get_ml();
}, false);
} else if (typeof el.attachEvent != "undefined") { //incase you support IE8
el.attachEvent("onkeyup", function(evt) {
get_ml();
});
}
<textarea id="ta_in" rows="7" cols="42" onkeyup="get_ml()"></textarea>