文档完成后,我目前正在执行以下操作。
var passwordInputs = document.querySelectorAll("input[type=password]");
for (index = 0; index < passwordInputs.length; ++index) {
passwordInputs[index].addEventListener("focusin", activeWordsFocusIn);
passwordInputs[index].addEventListener("focusout", activeWordsFocusOut);
}
这段代码能够如预期地工作。但是如果页面包含一些修改DOM并添加更多input
元素的附加脚本,它们就无法被钩住。
如何为所有input
元素添加事件处理程序,即使是通过脚本/ajax添加到DOM中的元素?
不是重复的问题我不认为这是一个重复的问题,因为这个问题Detect Changes in the DOM着重于检测DOM的变化。我的问题重点在于即使DOM更改,也要为所有input
元素添加eventListener
。我现在已经添加了自己的答案。