JQUERY IFRAME 父頁面子頁面互相查找


下面簡單使用Jquery來操作iframe的一些記錄,這個使用純JS與可以實現。

第一、在iframe中查找父頁面元素的方法:
$('#id', window.parent.document)

第二、在父頁面中獲取iframe中的元素方法:
$(this).contents().find("#suggestBox")

第三、在iframe中調用父頁面中定義的方法和變量:
parent.method
parent.value

 

iframe里用jquery獲取父頁面body
iframe.html

<html>
<script src='jquerymin.js'>
</script>
<body id='1'>
    <div>it is a iframe</div>
</body>    
<script>
    $(document).ready(
        function()
        {
           
        var c = $(window.parent.document.body) //麻煩的方法: var c = $($(window).eq(0)[0].parent.document).find('body'); ,忘了可以用前面的方法了
            
        alert(c.html());
        }
    );
    
</script>
</html>





content.html

<html>
    <script src='jquerymin.js'>
</script>
<body id='fa'>
    <iframe src='iframe.html' width='100' height='100'></iframe>
    <br>
        <br>
            <br>
            <div id='mydiv'>content iframe</div>
</body>    
<script>
    function a()
    {
        alert(1);
    }
    alert($(document).text());
    </script>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM