2012-05-15 101 views
4

我想獲得iframe的.text(),但它不起作用。該iframe在同一個域中。單擊一次,我將頁面加載到框架上,第二次我想在警報中顯示內容,但我得到空警報。jQuery:獲取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無法選擇外部元素,反之亦然。 –

+0

這是否重複? http://stackoverflow.com/questions/6316979/selecting-an-element-in-iframe-jquery – Greg

回答