简体   繁体   English

克隆节点上的DOM操作

[英]DOM manipulation on a cloned node

I have a query related to DOM manipulation. 我有一个与DOM操作相关的查询。 I am performing some operations on the DOM nodes in a web page by cloning the entire document node and then performing the operations on the cloned copy. 通过克隆整个文档节点,然后在克隆的副本上执行这些操作,我正在网页中的DOM节点上执行一些操作。

One particular operation that I am performing on the DOM is changing the 'src' attribute of the 'img' tags. 我在DOM上执行的一项特定操作是更改“ img”标签的“ src”属性。 When I do that, I see a 404 error (404 (Not Found)) in the console for that page in the browser. 这样做时,我在控制台中看到浏览器中该页面的404错误(404(未找到))。 The path I am setting is not present but since I am performing the operations on the cloned copy, I assumed that it should not show any error. 我正在设置的路径不存在,但是由于我正在克隆副本上执行操作,因此我认为该路径不应显示任何错误。 Am I doing something wrong here? 我在这里做错什么了吗? Is there a way to avoid this error from being displayed? 有没有办法避免显示此错误?

The code snippet is provided below: 下面提供了代码段:

//code snippet

var doc = document.documentElement.cloneNode(true);
var allNodes = doc.getElementsByTagName('*');

//for loop to interate through the node

//When an img node is found
node.src = "some_image.png";

From the comments... 从评论中...

I can't see why it should'nt load. 我看不到为什么不应该加载。 Because var img = new Image(); 因为var img = new Image(); img.src = "aa.png" also loads a image, even though the image is not appended on the DOM. img.src =“ aa.png”也会加载图像,即使该图像未附加在DOM上也是如此。 (This approach is often used in HTML5 Canvas usages) (这种方法通常在HTML5 Canvas用法中使用)

Even though the image is added to the path after the src is defined, the image will not be found, because the browser will only send 1 request for the file. 即使在定义src之后将图像添加到路径,也不会找到该图像,因为浏览器将仅发送1个文件请求。

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

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