我如何在JavaScript中获得这个子元素html元素? [英] How do I get this child html element in JavaScript?

查看:71
本文介绍了我如何在JavaScript中获得这个子元素html元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的iframe:

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

我试图获取 iframe html 标记。
在JavaScript中,当我这样做时:

  document.getElementByID(iframe2)

这将返回正确的iframe 。

但是,当我这样做时:

  document.getElementByID (iframe2)。childNodes 

返回值是 [] $ b $ p $ document $ getElementsByTagName(#document)和 document.getElementByID(iframe2)。getElementsByTagName(html)也返回 []



如何访问 html 标签?

另外,那是什么 #document 标记调用?

解决方案

  document.getElementByID iframe2)。contentWindow.document 

或者,旧版本不支持IE,

  document.getElementByID(iframe2)。contentDocument 

这将使您获得嵌入式页面的文档对象,并从那里您可以使用 .documentElement .body .head 属性来获取html / body / head DOM。



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



MDN有指导 iframe scripting ,您可能会发现它很有帮助。


I have an iframe that looks like this:

<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:

document.getElementByID("iframe2")

this returns the correct 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 [].

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

解决方案

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

Or, the variant not supported by older 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.

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

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

这篇关于我如何在JavaScript中获得这个子元素html元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆