javascript - document.frame 在 ie11 中不起作用

标签 javascript

在没有兼容模式的 ie 11 中执行时,出现 undefined object 错误。它在 ie 11 兼容模式下工作正常。

示例代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script>
        function test() {
            var parent = document.getElementById("tabPaneMain");
            var doc = document;
            var html = '<div class="tab-page" id="tabCust" style="width: 100%; height: 95%">'
                    + '<h2 class="tab">Customer</h2>'
                    + '<table cellpadding=2 cellspacing=0 width="100%" height="100%">'
                    + '<tr><td valign=top id=iframeparent_tabCust>'
                    + '<iframe name="iframe_tabCust" id="iframe_tabCust" src="overview.html" style="width=100%; height: 100%;" scrolling="no" frameborder=0></iframe>'
                    + '</td></tr></table></div>';

            var node = doc.createElement("DIV");
            node.innerHTML = html;
            parent.appendChild(node);
            var test = node.document.frames["iframe_tabCust"];// undefined error: when execute in ie 11 without compatibile mode

        }
    </script> 
</head>
<body>
    <div class="tab-pane" id="tabPaneMain" style="left: 10px; height: 100%" />
    <button id="btn" type="button" onclick="test();">
        click
    </button> 
</body> 
</html>

提前致谢

最佳答案

您的 node 变量是一个 div 元素。上次我检查过,div 没有 document 属性。因此 node.document 将是未定义的,因此 node.document.frames 将是一个错误。

window.frames 是获取框架列表的标准位置。

<小时/>

由于您的框架上有一个唯一的 ID,因此获取框架的最简单且“支持跨浏览器”的方法可能是:

document.getElementById("iframe_tabCust")

关于javascript - document.frame 在 ie11 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29394435/

相关文章:

javascript - 如何使用 JQuery 获取随机 id 的值?

javascript - 如何在vuejs中从javascript导出数据?

javascript - 为什么 onload 事件不能作为我的 DIV 的事件起作用?

javascript - Vert.x 相当于 Node.js 全局对象

javascript - 如何使用 JS 和第二个单击处理程序关闭下拉菜单

javascript - 在 postman 中,我如何根据另一个测试是通过还是失败来运行新测试?

单击按钮时 JavaScript classList 不起作用

javascript - 在 Google Apps 脚本中从服务器端通信到客户端

javascript - 验证该字符串是正则表达式模式的开头

javascript - 如何在图标单击上打开 bootstrap-datepicker 而不仅仅是在输入单击上