Javascript 来自 iframe 的 getElementById

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1896360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 21:47:50  来源:igfitidea点击:

getElementById from iframe

javascript

提问by Phil Hymanson

could someone help me to understand why this errors

有人能帮我理解为什么会出现这个错误吗

document.getElementById("actContentToGet").contentWindow.document.body.getElementById is not a function

document.getElementById("actContentToGet").contentWindow.document.body.getElementById 不是函数

function deleteElement(element){
        var elementID = $(element).attr("class");
        alert(elementID);
        document.getElementById('actContentToGet').contentWindow.document.body.getElementById(elementID).remove;
        alterContent();
        giveAllIDs();
        hoverLoad();
    }

回答by nickf

Try changing this:

尝试改变这个:

...contentWindow.document.body.getElementById(elementID)...

to this:

对此:

...contentWindow.document.getElementById(elementID)...


Edit from comments: It's not removing that element because that's not how you remove elements. Try this:

从评论中编辑:它不会删除该元素,因为这不是您删除元素的方式。尝试这个:

var iframe = document.getElementById('actContentToGet');
var frameDoc = iframe.contentDocument || iframe.contentWindow.document;
var el = frameDoc.getElementById(elementID);
el.parentNode.removeChild(el);

See the documentation here.

请参阅此处文档

回答by Pekka

Try removing the body.- getElementById() is a document.function.

尝试删除body.- getElementById() 是一个document.函数。