如何使用document.getElementById选择iframe中的元素 [英] How to pick element inside iframe using document.getElementById

查看:139
本文介绍了如何使用document.getElementById选择iframe中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 iframe 就像这样

 < iframe名称=myframe1id =myframe1width =100%height =100%src =a.html> 
< html>
< head>< / head>
< frameset name =myframe2cols =0%,100%border =0frameBorder =0frameSpacing =0>
< frame name =page1src =c.htmlscrolling =no>< / frame>
< frame name =page2src =d.html>
< html>
< head>< / head>
< body id =top>
< div id =div1>
< div id =div2>
< div id =div3>
< ul id =x>
< li> a< / li>
< li> b< / li>
< / ul>
< / div>
< / div>
< / div>
< / body>
< / html>

< / frame>

< / frameset>
< / html>
< / iframe>

我想引用元素x。我尝试了几种方法,但我找不到解决方案。

解决方案

  document.getElementById('myframe1')。contentWindow.document.getElementById(' x')

小提琴





contentWindow 。 >请注意,如果 iframe src 来自其他域,您将无法访问其内容由于同源政策


I have a iframe like this

<iframe name="myframe1" id="myframe1" width="100%" height="100%" src="a.html">
<html>
    <head></head>
    <frameset name="myframe2" cols="0%, 100%" border="0" frameBorder="0" frameSpacing="0">
        <frame name="page1" src="c.html" scrolling="no"></frame>
        <frame name="page2" src="d.html" >
            <html>
                <head></head>
                <body id="top">
                    <div id="div1">
                        <div id="div2">
                            <div id="div3">
                                <ul id="x">
                                    <li>a</li>
                                    <li>b</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </body>
            </html>

        </frame>

    </frameset>
</html>
</iframe>

I want to refer to the element "x". I tried in several ways but I couldn't find a solution.

解决方案

document.getElementById('myframe1').contentWindow.document.getElementById('x')

Fiddle

contentWindow is supported by all browsers including the older versions of IE.

Note that if the iframe's src is from another domain, you won't be able to access its content due to the Same Origin Policy.

这篇关于如何使用document.getElementById选择iframe中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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