如何访问iframe元素? [英] How to access to iframe element?

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

问题描述

当你在这样的页面中循环遍历所有iframe:

When you have a loop through all iframes in a page like this:

HTML:

<ul>
    <li id="a"><iframe src="..." /></li>
    <li id="b"><iframe src="..." /></li>
    <li id="c"><iframe src="..." /></li>
</ul>

JS:

    for (var i = 0;  i < window.frames.length;  i++) {
        if (window.frames[i].getName() == selector) {
            frame_item = ????
            break;
        }
    }

如何获取< IFRAME>?或者按照示例,我如何获得<的ID? li>循环内的项目?

How can I get the DOM element for the < iframe>? Or following the example, how can I get the ID for the < li> item inside the loop?

推荐答案

更新回答。

window.frames直接引用IFRAME上下文(iframe的窗口对象)。 AFAIK你不能通过这种方式获得IFRAME对象。您可以通过 window.frames [0] .myVar 访问IFRAME的变量。

window.frames references directly to the IFRAME context (iframe's window object). AFAIK you cannot obtain IFRAME object by this way. You can access IFRAME's variables via window.frames[0].myVar.

要使用所有IFRAME节点/对象:

To get all IFRAME node/objects use:

var iframes = document.getElementsByTagName('iframe'); //all iframes on page
for(var i=0; i<iframes.length; i++){
    alert(iframes[i].parentNode.id); // LI.id
    alert(iframes[i].contentWindow.myVar); //iframe's context
}

或者你可以为UL元素分配id和

Alternatively you can assign id to UL element and

var ul = document.getElementById('ul_id');
var iframes = ul.getElementsByTagName('iframe'); //all iframes in UL
...

这篇关于如何访问iframe元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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