简体   繁体   English

Javascript appendChild 不在 DOM 中反映

[英]Javascript appendChild does not reflect in the DOM

I have a requirement to insert an image tag inside an iframe.我需要在 iframe 中插入图像标签。 When I debug the code I can see the added html in the object.当我调试代码时,我可以在 object 中看到添加的 html。 But it does not show up in the DOM nor can I see it on the page This is a snippet for reference.但它没有出现在 DOM 中,我也无法在页面上看到它。这是一个供参考的片段。

var tdForImage = iframewindow.document.getElementsByTagName("table")[1].getElementsByTagName('tr')[0].getElementsByTagName('td')[0];
var imgTag = document.createElement("img");

//copying img src value from elsewhere
imgTag.src = iframewindow.document.getElementsByTagName("img")[0].src;

//Remove the wrongly placed second image.
iframewindow.document.getElementsByTagName("img")[0].parentNode.removeChild(iframewindow.document.getElementsByTagName("img")[0]);
tdForImage.appendChild(imgTag);

After this I can see that there is no change in the UI.在此之后,我可以看到 UI 没有任何变化。 The iframe is BLANK. iframe 为空白。 In its document there is nothing in the head nor in the body.在它的文件中,头部和身体都没有。 While debugging, I can see img inside innerHTML attr in tdForImage.在调试时,我可以在 tdForImage 的 innerHTML attr 中看到 img。 Why does the iframe get cleared?为什么 iframe 会被清除? If anyone has some experience of such issues please advise..如果有人对此类问题有一些经验,请告知..

EDIT 1 >> I have noticed that on IE 11, the script added to the iframes head tag is not loaded when the page is loaded for the first time.编辑 1 >> 我注意到在 IE 11 上,添加到 iframe 头标记的脚本在第一次加载页面时没有加载。 This seems to be the root cause of the issue.这似乎是问题的根本原因。 I have already tried adding a 2 second timeout.我已经尝试添加 2 秒超时。 I have already tried loading the script containing function as innerHTML.我已经尝试将包含 function 的脚本加载为 innerHTML。 Both are not working.两者都不起作用。 If anyone knows a way to force IE to load the scripts immediately after the appendChild call please let me know..如果有人知道在 appendChild 调用后强制 IE 立即加载脚本的方法,请告诉我..

It works for me on JSFiddle:它在 JSFiddle 上对我有用:

<iframe id="frame" src="about:blank"></iframe>
// Fill iframe for demo
document.getElementById('frame').contentWindow.document.write('<table><tr><td>fdsafdsa</td></tr></table><img src="https://placekitten.com/200/300" />');

var iframe = document.getElementById('frame');

var iframewindow = iframe.contentDocument || iframe.contentDocument.document;
console.log(iframewindow.toString());

var tdForImage = iframewindow.getElementsByTagName("table")[0].getElementsByTagName('tr')[0].getElementsByTagName('td')[0];
var imgTag = document.createElement("img");

imgTag.src = iframewindow.getElementsByTagName("img")[0].src;
iframewindow.getElementsByTagName("img")[0].parentNode.removeChild(iframewindow.getElementsByTagName("img")[0]);

tdForImage.appendChild(imgTag);

https://jsfiddle.net/r6o83gL5/12/ https://jsfiddle.net/r6o83gL5/12/

But if your iframe is on a different domain, you will not be allowed to access it's body.但是,如果您的 iframe 位于不同的域中,则将不允许您访问它的主体。

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

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