获取iframe中的元素

在父窗口中获取iframe中的元素

index.html

<div id="parents">
    <div class="dome" style="background: red;">parents页面</div>
    <iframe id="childrens" src="iframe.html" width="100%">
                        
    </iframe>
    <div class="btn">获取iframe页面</div>
</div>
<script type="text/javascript">
  //父页面获取iframe中的元素
  $(".btn").click(function(){
       var txt=$("#childrens").contents().find(".inner").html();
          alert("parents:"+txt);
   });
 </script>

在iframe中获取父窗口的元素

iframe.html

<div id="outer" style="background: green;">
    <div class="inner">iframe页面</div>
</div>
<div class="btn">获取父页面</div>
<script type="text/javascript">
    //获取iframe福页面中的元素
    $(".btn").click(function(){
        var txt=$(".dome",parent.document).text();
            alert("iframe:"+txt);
    });
</script>

猜你喜欢

转载自www.cnblogs.com/beli/p/9483103.html
今日推荐