jQuery:获取iframe内容,同域

4

我想获取一个iframe的 .text(),但是它不起作用。该iframe与当前页面在同一域中。点击一次后,页面加载到了iframe中,第二次点击时,我想在警告框中显示内容,但是我得到的是空的警告框。

<html> 
 <head> 
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 

 <script type="text/javascript"> 

$(document).ready( function() {
        // load simple paage to the frame
    $("#load").click( function(){

        $("<iframe id='theframe' width='400' // single line
          height='400' src='http://localhost/dummy.html'/>").appendTo('body');

     } );

        // display the content - but I get empty alert
    $('#content').click( function() {
    var content;
    content = $("#theframe").text();
    alert(content);
    });
});

 </script> 
 </head> 
 <body> 

<a id='load' href="#">load</a>
<a id='content' href="#">check content</a>

 </body> 
 </html>

1
据我所知,这是不可能的,因为它是另一个DOM。iFrame内部的JS无法选择外部元素,反之亦然。 - Johannes Klauß
这个是否是重复的问题?https://dev59.com/Im015IYBdhLWcg3w6QLA - Greg
1个回答

6

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接