解決iframe嵌套進來的頁面操作父頁面元素加載新頁面的問題

一.場景:

<!-- 這是load頁面的標籤 -->
<div class="side-right RIG-PAGE mgl-20" id="load-op" style="display: none; top: 20px"></div> 

<!-- 這是iframe頁面的標籤 -->
<div class="side-right RIG-PAGE mgl-20" id="iframe-op" style="display: none; top: 20px"> 
        <iframe class="iframe" id="iframe" name="iframe" src="" scrolling="no"></iframe> 
</div>

當我將頁面iframe進去後   想在iframe引進來的頁面中的js   操作父頁面id爲load-op的標籤進行加載頁面   

發現$("#load-op").load("test.html")不起作用

 

二.解決:

1.加個聲明:$("#load-op",parent.document).load("test.html");

2.由於我項目中還使用了requirejs 這種方式雖然可行能生效  但還是存在問題  於是又換了種方法

首先建立一個空白頁

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
</body>
<script src="/nresource/js/lib/jquery-1.11.3.min.js"></script>
<script src="/nresource/commons/marsatg/common-utils.js"></script>
<script type="text/javascript">
//服務器端發送消息
$(function(){
   window.οnlοad=function(){
      var type = $.trim(getParam("type"));
      if(type=="item"){//商品
           window.parent.postMessage('item',"/admin/home.html");
      }
    }
})
</script>
</html>

 在iframe頁面的js中跳轉這個空白頁 

window.location.href="/admin/commontransfer.html?type=item";

在父頁面中加上

//添加監聽,解決iframe跨域傳輸問題
window.addEventListener('message', function(e) {
                              var type = e.data;
                              $("#load-op").hide();
                              $("#iframe-op").hide();
                              $("#iframe").html("");
                              if (type == "item") {
                                    $("#load-op").show();
                                    $.get("/open/mall/item/item.html",
                                    function(html) {
                                        $("#load-op").html(html);
                                    });
                               }
                           }, false);

 如此即可...

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章