本篇文章主要是對javascript擷取iframe裡頁面中元素值的方法進行了介紹,需要的朋友可以過來參考下,希望對大家有所協助
IE方法:document.frames['myFrame'].document.getElementById('test').value; Firefox方法:document.getElementById('myFrame').contentWindow.document.getElementById('test').value; IE、Firefox方法: 代碼如下: function getValue(){ var tmp = ''; if(document.frames){ tmp += 'ie哥說:'; tmp += document.frames['myFrame'].document.getElementById('test').value; }else{ tmp = document.getElementById('myFrame').contentWindow.document.getElementById('test').value; } alert(tmp); } 範例程式碼:a.html頁面中的代碼 代碼如下:<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title> javascript 擷取iframe裡頁面中元素的值 測試 </title> </head> <body> <iframe id="myFrame" src='b.html' style="width:300px;height: 50px;"></iframe> <input type="button" id="btn" onclick="getValue()" value="test" > <script type="text/javascript"> function getValue(){ var tmp = ''; if(document.frames){ tmp += 'ie哥說:'; tmp += document.frames['myFrame'].document.getElementById('test').value; }else{ tmp = document.getElementById('myFrame').contentWindow.document.getElementById('test').value; } alert(tmp); } </script> </body> </html> b.html頁面中的代碼代碼如下: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title> 我是 iframe內的頁面 </title> </head> <body> <input type='text' id="test" value='歡迎訪問:justflyhigh.com'> </body> </html>