【问题标题】:Get element value inside iframe which is nested inside Frame in javascript?获取嵌套在javascript框架内的iframe内的元素值?
【发布时间】:2014-07-27 07:15:47
【问题描述】:

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

我试过这样:

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

但我没有收到任何价值,尽管它在那里。

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

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

【问题讨论】:

  • 两个网址是否在同一个域中?
  • 是的,两个网址都在域中
  • 在帧之间传递信息的一种有趣方式是window.LocalStorage()
  • 还有其他方法吗?
  • This answer 应该对您有所帮助,但这并不是您问题的完美答案。

标签: javascript iframe frame getelementbyid


【解决方案1】:

这是我在评论中链接的修改后的 sn-p。如果未找到(i)frame,则函数返回传递id (id) 或null 的(i) 帧的window 对象。仅当所有(i)frames 都在同一个域中时,此方法才有效。此外,赋予(i)frame 元素的ids 在窗口结构中涉及的所有 文档中必须是唯一的。

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
}

这个函数可以用传递的id找到frameiframe,不管它放在窗口结构的什么位置。该函数也可以在任何窗口中放置和调用。我会把它放到主页上(作为全局)。如果子窗口中需要该方法,只需使用top.searchFrame(...) 调用即可。

除了ids,也可以使用names,只要它们也是唯一的。在这种情况下,searchFrame() 中的id 签入需要编辑为name 签入。

在你的情况下,首先你需要给目标iframe一个id,然后你可以像这样使用代码:

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

上述方法获取单个引用可能有点矫枉过正,但如果您必须在不同窗口中多次获取这些跨窗口引用,它会非常有用。

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

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

(i)framenames 也可以方便地与frames 集合一起使用。相反,您可以使用名称索引。始终从主页开始链接引用,即从top。例如:

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

有用的阅读:

window.top
window.frameElement
window.frames

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多