简体   繁体   English

突变观察者-获取孩子的所有视频标签

[英]Mutation observer - get all video tags of childs

I have a mutation observer (subtree: true). 我有一个变异观察者(子树:true)。 I shows all nodes added (I guess). 我显示了所有添加的节点(我想)。 When I monitor it with console.log(mutation.target.tagName) I see DIV and BODY and so on... HoweverI I need to add a listener now to all added tags (in fact one for onPlay and one for onPause) 当我使用console.log(mutation.target.tagName)监视它时,我会看到DIV和BODY等...但是我现在需要向所有添加的标签添加一个侦听器(实际上一个监听器用于onPlay,一个监听器用于onPause)

Any idea how I can find (and react on) all added video tags in the childNodes added? 知道如何在添加的childNode中找到(并做出反应)所有添加的视频标签吗?

var target = document.body;
//then define a new observer
var bodyObserver = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
    //How do i egt the video tags to add a listener?
    console.log("mutation detected: " + mutation.target.tagName);
    if (mutation.target.tagName == "VIDEO") {

        mutation.target.onclick =function () { console.log("The video has been clicked")}
    }
})
})
var bodyObserverConfig = { attributes: true, childList: true, subtree: true, 
characterData: true };
bodyObserver.observe(target, bodyObserverConfig);

The added nodes are listed in addedNodes property of a MutationRecord ( https://developer.mozilla.org/en-US/docs/Web/API/MutationRecord ) ( mutation in your case). 添加的节点列在MutationRecordhttps://developer.mozilla.org/en-US/docs/Web/API/MutationRecord )的addedNodes属性中(在您的情况下为mutation )。 You have to iterate through this list and search for video nodes. 您必须遍历此列表并搜索视频节点。
Here is an example: 这是一个例子:

If stackoverflow code snippet does not work - try codepen https://codepen.io/bsalex/pen/WMGvry . 如果stackoverflow代码段不起作用-尝试codepen https://codepen.io/bsalex/pen/WMGvry

 var target = document.body; //then define a new observer function addVideoHandler(node) { if (node.tagName == "VIDEO") { node.onclick = function() { alert("The video has been clicked"); }; } } var bodyObserver = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { //How do i egt the video tags to add a listener? console.log("mutation detected: " + mutation.target.tagName); mutation.addedNodes.forEach(addedNode => { addVideoHandler(addedNode); // it might be text node or comment node which don't have querySelectorAll addedNode.querySelectorAll && addedNode.querySelectorAll("video").forEach(addVideoHandler); }); }); }); var bodyObserverConfig = { attributes: true, childList: true, subtree: true, characterData: true }; bodyObserver.observe(target, bodyObserverConfig); // Example tools code below document .querySelector(".add-videos-here-action") .addEventListener("click", () => { const videoElement = document.createElement("video"); document.querySelector(".add-videos-here").appendChild(videoElement); }); document .querySelector(".add-nested-videos-here-action") .addEventListener("click", () => { const wrappingElement = document.createElement("div"); wrappingElement.innerHTML = "<div><p><video /></p></div><div><video /></div>"; document.querySelector(".add-videos-here").appendChild(wrappingElement); }); document .querySelector(".add-not-videos-here-action") .addEventListener("click", () => { const notVideoElement = document.createElement("div"); notVideoElement.textContent = "It is not a video"; document.querySelector(".add-videos-here").appendChild(notVideoElement); }); document .querySelector(".add-videos-here-too-action") .addEventListener("click", () => { const videoElement = document.createElement("video"); document.querySelector(".add-videos-here-too").appendChild(videoElement); }); 
 video { min-width: 100px; min-height: 100px; border: 1px solid red; } 
 <button class="add-videos-here-action">Add video</button> <button class="add-not-videos-here-action">Add NOT video</button> <button class="add-videos-here-too-action">Add another video</button> <div> <div class="add-videos-here"> </div> <div class="add-videos-here-too"> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM