iframe.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <a href="javascript:;" onclick="return getIframeDocument()">get iframe input value</a> <div class="content" id="content"> <iframe scrolling="no" frameborder="0" src="inner.html" class="comnet" name="comnet" id="comnet"></iframe> </div> <script type="text/javascript"> function getIframeDocument(){ var iframe = document.getElementById('comnet'); var iframeDocument = iframe.contentDocument || iframe.contentWindow.document; if(iframeDocument){ alert(iframeDocument.getElementById('first').value); }else{ alert("oops!"); } return false; } window.onload=function(){ //取iframe var iframe = document.getElementById('comnet'); var iframeDocument = iframe.contentDocument || iframe.contentWindow.document; if(iframeDocument){ alert(iframeDocument.getElementById('first').value); }else{ alert("oops!"); } }; </script> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>this is iframe document</title> </head> <body> <div id="mainright">ddd</div> <div><input type="text" id="first" value="ccc"/></div> </body> </html>
iframe.contentDocument || iframe.contentWindow.document
var iframe = window.frames["comnet"]; if(iframe.document){ alert(iframe.document.getElementById('first').value); }else{ alert("oops!"); }
对白三: firefox我忠贞如一
上面的ie10问题,chrome问题,在firefox上都没问题
参考连接:各浏览器Iframe对contentWindow、contentDocument、document及frames属性测试