鍍金池/ 問答/HTML/ JavaScript:關于跨iframe彈窗

JavaScript:關于跨iframe彈窗

index.html

<html>
<head>
<script type="text/javascript" src="easyui/jquery.min.js"></script>
</head>
<body>
    <iframe src="iframe.html"></iframe>
</body>
</html>

iframe.html

<html>
<head>
<link rel="stylesheet" type="text/css" href="easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="easyui/themes/icon.css">
<script type="text/javascript" src="easyui/jquery.min.js"></script>
<script type="text/javascript" src="easyui/jquery.easyui.min.js"></script>
<script type="text/javascript">
    $(function(){
         $("#window").window({
            width: 200,
            height: 100
        }); 
    });
</script>
</head>
<body>
    <div id="window"></div>
</body>
</html>

效果
圖片描述

由于是在iframe中彈窗致使窗口被腰斬,所以需要向window中追加節(jié)點再彈窗,解決方式如下

    var win = window.top.document.createElement("div");
    window.top.document.body.appendChild(win);
    window.top.$(win).window({
            width: 200,
            height: 100
    });
    window.top.$(win).window("open");

但在實際項目中無法向index.html引入easyui相關類庫,所以會報錯
圖片描述

什么call、apply、__proto__之類的辦法都想過了,不知道該怎么辦了...

====================臨時解決辦法==========================

<html>
<head>
<link rel="stylesheet" type="text/css" href="easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="easyui/themes/icon.css">
<script type="text/javascript" src="easyui/jquery.min.js"></script>
<script type="text/javascript" src="easyui/jquery.easyui.min.js"></script>
<script type="text/javascript">
    $(function() {

        var script = parent.document.createElement("script");
        script.type = "text/javascript";
        script.src = "easyui/jquery.easyui.min.js";
        parent.document.getElementsByTagName("head")[0].appendChild(script);

        var link1 = parent.document.createElement("link");
        link1.rel = "stylesheet";
        link1.type = "text/css";
        link1.href = "easyui/themes/default/easyui.css";
        parent.document.getElementsByTagName("head")[0].appendChild(link1);
        
        var link2 = parent.document.createElement("link");
        link2.rel = "stylesheet";
        link2.type = "text/css";
        link2.href = "easyui/themes/default/easyui.css";
        parent.document.getElementsByTagName("head")[0].appendChild(link2);
        
    });
    
    function click() {
        var win = parent.document.createElement("div");
        parent.document.body.appendChild(win);
        parent.$(win).window({
            width : 200,
            height : 100
        });
        parent.$("#div").window("open");
    }
</script>
</head>
<body>
    <input type="button" onclick="click()" />
</body>
</html>
回答
編輯回答
萌面人

試了一下,easyui不支持跨頁面,但你可以動態(tài)地在top中引入easyui再打開window,或者你可以自己在top里手動畫一個window。其次,你的第二張圖報的錯是因為你的top中沒有引入jQuery,你可以top.$ = $;,但沒什么卵用,創(chuàng)建的window會在iframe里。另外Google Chrome是沒有你的第一張圖的問題的。

2017年10月29日 15:30