简体   繁体   English

如何清除iframe中的元素?

[英]How do I clear an element inside an iframe?

I have a span element 我有一个span元素

<span>Test</span>

inside an iframe. 在iframe中。

How do I clear that element using vanilla javascript? 如何使用香草javascript清除该元素? Remove the html/text inside it so it becomes 删除其中的html / text,使其成为

<span></span>

NOTE: THE SPAN IS INSIDE AN IFRAME 注意:SPAN位于一个框架内

Here's my code: 这是我的代码:

var clearElement = function (fid) {
    document.querySelector(fid).innerHTML = "";
};
document.getElementsByTagName('iframe')[0].contentWindow.clearElement(fid);

What you are doing is trying to somehow call a function inside of the iframe which is not you have. 您正在做的是尝试以某种方式在iframe中调用您没有的函数。 First the iframe needs to be in the same domain. 首先,iframe必须位于同一域中。 Second, you need to use the DOM of the iframe, not the current window 其次,您需要使用iframe的DOM,而不是当前窗口

var iframe = document.getElementById('iframeId'); 
//var iframe = document.getElementsByTagName('iframe')[0];
var content = iframe.contentDocument || iframe.contentWindow.document;
content.getElementById("theId").innerHTML = "";

Now if the span does not have an id, than we need more context on how to select the span. 现在,如果跨度没有ID,那么我们需要更多有关如何选择跨度的上下文。 querySelector("span") will get the first span. querySelector("span")将获得第一个跨度。

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

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