如何在JavaScript中获得子html元素?

时间:2022-10-30 10:42:05

I have an iframe that looks like this:

我有一个iframe,看起来是这样的:

<iframe id="iframe2" ...>
    #document
        <html>...</html>
</iframe>

I am trying to get the item underneath the iframe with the html tag.
In JavaScript, when I do:

我正在尝试使用html标记获取iframe之下的项。在JavaScript中,当我这样做时:

document.getElementByID("iframe2")

this returns the correct iframe.

这将返回正确的iframe。

However, when I do this:

然而,当我这样做的时候:

document.getElementByID("iframe2").childNodes

the return is [].

返回的是[]。

document.getElementByID("iframe2").getElementsByTagName("#document") and document.getElementByID("iframe2").getElementsByTagName("html") also return [].

getelementbytagname(“#document”)和document. getelementbyid(“iframe2”). getelementsbytagname(“html”)也返回[]。

How do I access that html tag?
Also, what is that #document tag called?

我如何访问那个html标签?还有,那个#文档标签叫什么?

4 个解决方案

#1


5  

document.getElementByID("iframe2").contentWindow.document

Or, the variant not supported by older IE,

或者是老版本IE不支持的变体,

document.getElementByID("iframe2").contentDocument

This will get you the document object of the embedded page, and from there you can use .documentElement, .body or .head properties to get the html/body/head DOM.

这将为您提供嵌入页面的文档对象,并从那里可以使用. documentelement、.body或.head属性获取html/body/head DOM。

If you want the window object of the embedded page, use contentWindow instead of contentDocument.

如果您想要嵌入页面的窗口对象,请使用contentWindow而不是contentDocument。

MDN has a guide to iframe scripting that you will probably find helpful.

MDN提供了iframe脚本编写指南,您可能会发现它很有帮助。

#2


2  

Please try this code:

请试试这段代码:

var a=document.getElementById("iframe2").firstChild;

#3


0  

Does the URL for the IFrame content have the same domain as the parent page? If not, you won't be able to access anything inside the IFrame due to the same-origin policy.

IFrame内容的URL是否与父页面具有相同的域?如果没有,由于同源策略,您将无法访问IFrame中的任何内容。

#4


0  

Try this :

试试这个:

var a = document.getElementById("iframe2").getElementsByTagName("*")[0];

#1


5  

document.getElementByID("iframe2").contentWindow.document

Or, the variant not supported by older IE,

或者是老版本IE不支持的变体,

document.getElementByID("iframe2").contentDocument

This will get you the document object of the embedded page, and from there you can use .documentElement, .body or .head properties to get the html/body/head DOM.

这将为您提供嵌入页面的文档对象,并从那里可以使用. documentelement、.body或.head属性获取html/body/head DOM。

If you want the window object of the embedded page, use contentWindow instead of contentDocument.

如果您想要嵌入页面的窗口对象,请使用contentWindow而不是contentDocument。

MDN has a guide to iframe scripting that you will probably find helpful.

MDN提供了iframe脚本编写指南,您可能会发现它很有帮助。

#2


2  

Please try this code:

请试试这段代码:

var a=document.getElementById("iframe2").firstChild;

#3


0  

Does the URL for the IFrame content have the same domain as the parent page? If not, you won't be able to access anything inside the IFrame due to the same-origin policy.

IFrame内容的URL是否与父页面具有相同的域?如果没有,由于同源策略,您将无法访问IFrame中的任何内容。

#4


0  

Try this :

试试这个:

var a = document.getElementById("iframe2").getElementsByTagName("*")[0];