在javascript中嵌套在Frame内部的iframe中获取元素值? [英] Get element value inside iframe which is nested inside Frame in javascript?

查看:1223
本文介绍了在javascript中嵌套在Frame内部的iframe中获取元素值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我主要的php页面有2帧。在第二帧内有iframe。
我想从第1帧访问iframe文档中元素的值。

I main php page in which there are 2 frames. Inside the second frame there is iframe. I want to access value of element on iframe document from the 1st frame.

我试过这样:

var frame1 = parent.frames[1];
var frame2 = frame1.document.getElementsByTagName('iframe')[0];
var eleValue =frame2.contentWindow.document.getElementById("MyElement").value;

但是我没有收到任何价值。

but I am not receiving any value though it is there.

var frame1 = parent.frames[1];
alert(frame1.name);
var frame2 = frame1.document.getElementsByTagName('iframe')[0];
alert(frame2.name);
var txtClinic = frame1.document.getElementsByTagName('iframe')[0].contentDocument.getElementById("clinicFlag");

最后一行代码不返回任何控制对象。

last line of code doesnot return any control object.

推荐答案

这是我在评论中链接的修改后的片段。函数返回(i)帧的窗口对象,传递 id id (i)框架,则code>)或 null 。如果所有(i)frame 都在同一个域中,则此方法有效。同样 id 给予(i)frame 元素在所有文件中必须是唯一的在窗口结构中。

Here's a modified snippet of my answer linked in a comment. Function returns the window object of an (i)frame with id passed (id), or null, if the (i)frame is not found. This method works only, if all the (i)frames are in the same domain. Also ids given to (i)frame elements must be unique throughout all documents involved in the window structure.

function searchFrame(id) {                                     // id = the id of the wanted (i)frame
    var result = null,                                         // Stores the result
        search = function (iframes) {                          // Recursively called function
            var n;                                             // General loop counter
            for (n = 0; n < iframes.length; n++) {             // Iterate through all passed windows in (i)frames
                if (iframes[n].frameElement.id = id) {         // Check the id of the (i)frame
                    result = iframes[n];                       // If found the wanted id, store the window to result
                }
                if (!result && iframes[n].frames.length > 0) { // Check if result not found and current window has (i)frames
                    search(iframes[n].frames);                 // Call search again, pass the windows in current window
                }
            }
        };
    search(window.top.frames);                                  // Start searching from the topmost window
    return result;                                              // Returns the wanted window if found, null otherwise
}

此函数可以找到 frame iframe ,传递 id ,无论它在哪里放在窗户结构中。此功能也可以在任何窗口中放置和调用。我把它放到主页面(作为全局)。如果在子窗口中需要该方法,只需使用 top.searchFrame(...)进行调用。

This function can find a frame or iframe with the passed id regardless where it is placed in the window structure. Also the function can be placed and called in any window. I'd put this to the main page (as global). If the method is needed in subwindows, just call with top.searchFrame(...).

而不是 id s,也可以使用 name s,只要它们也是唯一的。在这种情况下,需要将 id 签入 searchFrame()编辑为 name check。

Instead of ids, also names can be used, as long as they are also unique. In that case the id check in searchFrame() needs to be edited to name check.

在您的情况下,首先您需要向目标<提供 id code> iframe ,然后您可以使用代码,例如:

In your case, first you need to give an id to the target iframe, then you can use the code for example like this:

var txtClinic = searchFrame('IFRAME_ID').document.getElementById('clinicFlag');






上述方法可能有点过分无法获得一个参考,但它非常有用,如果你必须在不同的窗口中多次获得这些跨窗口引用。


The method above might be a bit overkilling to get a single reference, but it's very helpful, if you have to get these cross-window references multiple times within different windows.

你的具体任务可以像这样完成:

The specific task of yours could be done like this:

var txtClinic = parent.frames[1].frames[0].document.getElementById('clinicFlag');






name (i)框架也可以方便地与 frames 集合一起使用。而不是索引,您可以使用名称。只需始终从主页开始链接引用,即从 top 开始。例如:


names of the (i)frame are also handy to use with frames collection. Instead indices you can use names. Just always chain the reference starting from the main page, i.e. from top. For example:

var iframeWin = top.frames['frame2'].frames['iframe1'];






有用的阅读:


Useful reading:

window.top

window.frameElement < br>
window.frames

这篇关于在javascript中嵌套在Frame内部的iframe中获取元素值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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