獲取iframe中的元素

在父窗口中獲取iframe中的元素javascript

index.htmlhtml

<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中獲取父窗口的元素java

iframe.htmldom

<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>
相關文章
相關標籤/搜索