在javascript中嵌套在框架内的iframe中获取元素值?

在javascript中嵌套在框架内的iframe中获取元素值?,javascript,iframe,frame,getelementbyid,Javascript,Iframe,Frame,Getelementbyid,我看到一个php主页,其中有两个框架。在第二个帧中有iframe。 我想从第一帧访问iframe文档中元素的值 我试着这样做: var frame1 = parent.frames[1]; var frame2 = frame1.document.getElementsByTagName('iframe')[0]; var eleValue =frame2.contentWindow.document.getElementById("MyElement").value; var txtClin

我看到一个php主页,其中有两个框架。在第二个帧中有iframe。 我想从第一帧访问iframe文档中元素的值

我试着这样做:

var frame1 = parent.frames[1];
var frame2 = frame1.document.getElementsByTagName('iframe')[0];
var eleValue =frame2.contentWindow.document.getElementById("MyElement").value;
var txtClinic = searchFrame('IFRAME_ID').document.getElementById('clinicFlag');
var txtClinic = parent.frames[1].frames[0].document.getElementById('clinicFlag');
但我没有收到任何价值,尽管它存在

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");

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

这里是我答案的一个修改片段,链接在注释中。函数返回iframe的窗口对象,如果找不到该iframe,则返回id为passed id或null的iframe。此方法仅适用于所有iFrame都位于同一域中的情况。此外,在窗口结构中涉及的所有文档中,指定给iframe元素的ID必须是唯一的

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
}
上面的方法获取单个引用可能有点过分,但是如果必须在不同的窗口中多次获取这些跨窗口引用,那么它非常有用

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

var frame1 = parent.frames[1];
var frame2 = frame1.document.getElementsByTagName('iframe')[0];
var eleValue =frame2.contentWindow.document.getElementById("MyElement").value;
var txtClinic = searchFrame('IFRAME_ID').document.getElementById('clinicFlag');
var txtClinic = parent.frames[1].frames[0].document.getElementById('clinicFlag');
iframe的名称也便于与frames集合一起使用。相反,您可以使用名称。只要始终从主页开始链接引用,即从顶部开始。例如:

var iframeWin = top.frames['frame2'].frames['iframe1'];
有用的阅读:


下面是我答案的一个修改片段,链接在一条评论中。函数返回iframe的窗口对象,如果找不到该iframe,则返回id为passed id或null的iframe。此方法仅适用于所有iFrame都位于同一域中的情况。此外,在窗口结构中涉及的所有文档中,指定给iframe元素的ID必须是唯一的

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
}
上面的方法获取单个引用可能有点过分,但是如果必须在不同的窗口中多次获取这些跨窗口引用,那么它非常有用

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

var frame1 = parent.frames[1];
var frame2 = frame1.document.getElementsByTagName('iframe')[0];
var eleValue =frame2.contentWindow.document.getElementById("MyElement").value;
var txtClinic = searchFrame('IFRAME_ID').document.getElementById('clinicFlag');
var txtClinic = parent.frames[1].frames[0].document.getElementById('clinicFlag');
iframe的名称也便于与frames集合一起使用。相反,您可以使用名称。只要始终从主页开始链接引用,即从顶部开始。例如:

var iframeWin = top.frames['frame2'].frames['iframe1'];
有用的阅读:


两个URL都在同一个域上吗?是的,两个URL都在域上。一个在帧之间传递信息的有趣方法是Windows。LocalStorages还有其他方法吗?这应该对你有所帮助,但这并不是你问题的完美答案。两个URL都在同一个域上吗?是的,两个URL都在域上。一个在帧之间传递信息的有趣方法是window。LocalStorages还有其他方法吗?应该会对你有所帮助,但这并不是你问题的完美答案。