10
09/2014
jquery访问iframe内容
先看这个 iframe 内外互访的例子
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(function(){
$('#showIframeContent').click(function(){
var info=$('body',$('#iframeId').contents()).html()
alert(info);
})
})
</script>
<iframe name="iframeId" id="iframeId" style="width:95%;border:solid 1px #ccc;" src="http://localhost/~david/jquery-iframe2.htm"></iframe>
<button id="showIframeContent">iframe内容</button>
更多方法:
var info=$('body',$('iframe').contents()).html()
var info=$('body',$('iframe').eq(0).contents()).html()
var info=$('body',$('iframe:nth-child(1)').contents()).html()
var info=$('body',$('iframe:first').contents()).html()
var info=$('body',$($('iframe')[0]).contents()).html()
var info=$('#iframeId').contents().find('body').html()