我知道如果我有一个元素触发一个事件,它会向上传播到根,然后返回到原始元素。基本上,所有的祖先都得到了通知。但我想通知所有的后代。我可以这样做吗?
谢谢,里克
最佳答案
这是可能的,我的代码可能有一些错误,但这里是:
elem = document.querySelectorAll('#my-element')[0];
function callClickOnDescendants(element, event){
for (var i = 0, len = element.children.length; i < len; i++){
element.children[i].onclick.call(element.children[i], event);
callClickOnDescendants(element.children[i], event);
}
}
elem.onclick = function(event){
callClickOnDescendants(elem, event);
}
如果您在投票前等待,我将能够在 jsfiddle 中进行测试。 编辑 它有效,jsfiddle proof
关于javascript - 我可以使事件在发起者和后代之间传播吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28680809/