jQuery:获取iframe内容,相同的域名

Jak*_* M. 4 iframe jquery

我想得到 .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>
Run Code Online (Sandbox Code Playgroud)

Gre*_*reg 6

取自:在iFrame jQuery中选择一个元素

var iframe = $('iframe'); // or some other selector to get the iframe
$('[tokenid=' + token + ']', iframe.contents()).addClass('border');`
Run Code Online (Sandbox Code Playgroud)