在包含iFrames的页面上使用document.getElementsByTagName - iframe中的元素未被拾取 [英] using document.getElementsByTagName on a page with iFrames - elements inside the iframe are not being picked up

查看:1135
本文介绍了在包含iFrames的页面上使用document.getElementsByTagName - iframe中的元素未被拾取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

较新的javascript。在我的页面(asp.net应用程序,vb作为服务器端语言),我使用document.getElementsByTagName(img)来获取所有img标记的html项目。我注意到一个特别没有出现在这个列表中(我能够将它们放入一个数组并单独访问这些项目 - 下面的代码)。这个项目包含在iframe中(iframe不使用SRC或其他任何东西,只是定义了它内部的html就在源代码中,不会从另一个页面加载 - 这是由生成的水晶报表查看器自动生成的在运行时,这就是为什么我无法复制显示这个)。 getElementsByTagName是否因为它在iframe中而没有拾取此图像?我需要能够访问这个img的src(图像是水晶报表查看器中的另一个自动生成的项目)。如果没有选择这个原因是因为iframe,有没有办法在iframe内的项目上运行它?或者只是获取iframe内部所有内容的innerhtml类型属性,以便可以访问img src。感谢您的帮助。

newer to javascript. On my page (asp.net application, vb as serverside language), I am using document.getElementsByTagName("img") to get all img tagged html items. I am noticing one in particular does not show up in this list (I was able to put them into an array and access the items individually - code below). This item is is contained inside an iframe (the iframe doesn't use SRC or anything else, just defined then the html inside it is right there in the source, doesnt load from another page - this is automatically generated by a crystal reports viewer generated at runtime, which is why I am unable to copy to show this). Is the getElementsByTagName not picking up this image because it is inside an iframe? I need to be able to access this img's src (the image is another auto generated item from the crystal report viewer). If the reason why this is not picked up is due to the iframe, is there any way to either run this on items inside the iframe? Or just get the "innerhtml" type property for everything inside the iframe so that the img src can be accessed. Thank you for your help.

var IMGmatches = [];
        var IMGelems = document.getElementsByTagName("img");
        for (var j = 0; j < IMGelems.length; j++) {
            IMGmatches.push(IMGelems[j]);
        }


推荐答案

那是因为元素在iframe不是父文档的一部分。

That's because elements that are in iframes are not a part of the parent document.

要获取iframe中的任何内容,您需要访问iframe的 contentDocument 。所以,你的代码可能如下所示:

To get anything inside an iframe, you need to access the iframe's contentDocument. So, your code might look like this:

var IMGmatches = [], IMGelems = document.getElementsByTagName("img"),
    iframes = document.getElementsByTagName('iframe'), l = IMGelems.length,
    m = iframes.length, i, j;
for( i=0; i<l; i++) IMGmatches[i] = IMGelems[i];
for( j=0; j<m; j++) {
    IMGelems = iframes[j].contentDocument.getElementsByTagName("img");
    l = IMGelems.length;
    for( i=0; i<l; i++) IMGmatches.push(IMGelems[i]);
}

这篇关于在包含iFrames的页面上使用document.getElementsByTagName - iframe中的元素未被拾取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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