使用 Javascript 设置 oninput 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9355499/
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
setting oninput event with Javascript
提问by Channel72
The HTML5 "oninput" event is supported by some modern browsers, including Firefox 3.X
一些现代浏览器支持 HTML5“oninput”事件,包括 Firefox 3.X
However, strangely, it only seems to work with inline javascript:
然而,奇怪的是,它似乎只适用于内联 javascript:
<input id = "q" oninput="alert('blah')">
When I try to set it using javascript code, it doesn't fire.
当我尝试使用 javascript 代码设置它时,它不会触发。
var q = document.getElementById("q");
q.oninput = function(){alert("blah");};
Is this just a bug in Firefox, or is there some reason this happens?
这只是 Firefox 中的一个错误,还是有什么原因会发生这种情况?
回答by Grant Zhu
After downloading FireFox v3.6.27 and doing some test and search. I found my previous answer was wrong.
下载 FireFox v3.6.27 并进行一些测试和搜索后。我发现我之前的答案是错误的。
What I got is:
我得到的是:
the oninput event property is supported in Firefox from version 4.
Firefox 从版本 4 开始支持 oninput 事件属性。
So to add a event listener in this case, you can do either
因此,要在这种情况下添加事件侦听器,您可以执行以下任一操作
<input id = "q" oninput="alert('blah')">
or
或者
q.addEventListener('input', function(){alert("blah");}, true);
But I prefer the later way. You can find reasons in addEventListener.
Also a similar function in IE attachEvent.
但我更喜欢后者。您可以在addEventListener 中找到原因。
在 IE attachEvent 中也有类似的功能。