1. 程式人生 > >父頁面內獲取獲取iframe內的變數或者是獲取iframe內的值

父頁面內獲取獲取iframe內的變數或者是獲取iframe內的值

前提:頁面不可跨域訪問,必須同一域名下,否則返回值為空

父頁面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="
http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <style> iframe{ position: absolute; top: 0; left: 0; width: 100%; height:80%; bottom: 80%; } .read{ position: absolute; bottom:
0; width: 100%; height: 20%; } </style> </head> <body> <!-- <iframe id="index" name="index" src="http://daogou.qionglegou.com/index.php?r=p/d&id=17331579&source=1l" frameborder="0"></iframe> --> <iframe id="index
" name="index" src="/content.html" frameborder="0"></iframe> <script> $(function(){ var oFrm = document.getElementById('index'); oFrm.onload = oFrm.onreadystatechange = function () { if (this.readyState && this.readyState != 'complete') { //alert("載入中。。。"); } else { var dom =$(window.frames["index"].document).find(".goods_reco").data('datasid'); console.log(dom) var c = $('#index')[0].contentWindow; console.log(c.goodsItem.goodsid); } } }) </script> </body> </html>

子頁面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
    <div class="goods_reco" data-datasid='123456'>1111111111</div>
    <script>
        var goodsItem = {goodsid:'123312312'};
    </script>
</body>
</html>