[英]Javascript/HTML Oninput with div
I've made oninput
for div
with a class. 我用一个类为
div
做了oninput
。 I can't add an ID to the div
. 我无法在
div
添加ID。 So here is my script: 所以这是我的脚本:
var objecto = document.getElementsByClassName(" nicEdit-main ")[0];
objecto.oninput = function() {
alert("Ok!");
}
Here is my HTML: 这是我的HTML:
<div class=" nicEdit-main " style="width: 499px; margin: 4px; min-height: 149px; overflow: hidden;" contenteditable="true">1123215545<br></div>
Why does oninput
not work for this div? 为什么
oninput
不适用于该div?
It should just work fine, make sure your script is executed after the target element is loaded in the dom. 它应该可以正常工作,请确保在将目标元素加载到dom后执行脚本。
1 way is to use the dom ready/window load event handler 一种方法是使用dom ready / window load事件处理程序
window.addEventListener('load', function() { var objecto = document.getElementsByClassName(" nicEdit-main ")[0]; objecto.oninput = function() { alert("Ok!"); } })
<div class=" nicEdit-main " style="width: 499px; margin: 4px; min-height: 149px; overflow: hidden;" contenteditable="true">1123215545 <br> </div>
Another is to place the script after the element like 另一种是将脚本放置在元素之后
<div class=" nicEdit-main " style="width: 499px; margin: 4px; min-height: 149px; overflow: hidden;" contenteditable="true">1123215545 <br> </div> <script> var objecto = document.getElementsByClassName(" nicEdit-main ")[0]; objecto.oninput = function() { alert("Ok!"); } </script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.