如何从另一个iFrame中清除iFrame的内容 [英] How to clear the contents of an iFrame from another iFrame

查看:127
本文介绍了如何从另一个iFrame中清除iFrame的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 wrapperPage.html ,其中< iframe class =header< iframe class =pageBody。在中有一个链接,< a class =clearLink,点击时应清除 pageBody



到目前为止,上述想法的以下实现不起作用。请帮我解决这个问题。



请注意 头和 pageBody 都是从不同的包含文件中加载的。
$ b wrapperPage.html

 < div class = non-floater> 
< iframe class =headersrc =header.html>< / iframe>
< iframe class =pageBodysrc =pageBody.html/>
< / div>

header.html:

 < script type =text / javascript> ()。
$(document).ready(function(){
$(。clearLink)。on(click,function(){
$('。pageBody')。contents ().find(body)。html('');
});
});
< / script>

< a class =clearLinkhref =#>导航按钮< / a>

pageBody.html:

 < div class =panel-body>这是body< / div> 


解决方案

尝试使用频道短信

wrapperPage.html

 < body> 
< div class = non-floater>
< iframe class =headersrc =header.html>< / iframe>
< iframe class =pageBodysrc =pageBody.html/>
< / div>
< script>
var channel = new MessageChannel();
var header = $(。header)[0] .contentWindow;
var pageBody = $(。pageBody)[0] .contentWindow;
header.onload = function(){
this.postMessage(,*,[channel.port2])
};

channel.port1.onmessage = function(e){
if(e.data ===clicked){
$(pageBody.document.body).html ()
}
}
< / script>
< / body>

header.html

 < body> 
< a class =clearLinkhref =#>导航按钮< / a>
< script>
var port;

onmessage = function(e){
port = e.ports [0]; (click,function(e){
port.postMessage(clicked);
}

$(。clearLink );
< / script>
< / body>


I have a wrapperPage.html with an <iframe class="header" and <iframe class="pageBody". In header there is a link, <a class="clearLink", which when clicked should clear the contents of pageBody.

So far the following implementation of the above idea doesn't work. Please help me resolve this.

Please NOTE that, header and pageBody are each loaded from different included files.

wrapperPage.html

<div class=non-floater>
    <iframe class="header" src="header.html"></iframe>
    <iframe class="pageBody" src="pageBody.html" /> 
</div>

header.html :

<script type="text/javascript">
    $(document).ready(function() {
        $(".clearLink").on("click", function() {
            $('.pageBody').contents().find("body").html('');
        });
    });
</script>

<a class="clearLink" href="#">Navigation Button</a>

pageBody.html :

<div class="panel-body">This is the body</div>

解决方案

Try using Channel messaging

wrapperPage.html

<body>
<div class=non-floater>
    <iframe class="header" src="header.html"></iframe>
    <iframe class="pageBody" src="pageBody.html" /> 
</div>
<script>
  var channel = new MessageChannel();
  var header = $(".header")[0].contentWindow;
  var pageBody = $(".pageBody")[0].contentWindow;
  header.onload = function() {
    this.postMessage("","*", [channel.port2])
  };

  channel.port1.onmessage = function(e) {
    if (e.data === "clicked") {
      $(pageBody.document.body).html("")
    }
  }
</script>
</body>

header.html

<body>
<a class="clearLink" href="#">Navigation Button</a>
<script>
  var port;

  onmessage = function(e) {
    port = e.ports[0];
  }

  $(".clearLink").on("click", function(e) {
      port.postMessage("clicked");
  });
</script>
</body>

这篇关于如何从另一个iFrame中清除iFrame的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆