开发者

jQuery.ajax how to receive data from another page's div

I am new studing jq开发者_如何学Pythonuery.ajax. I still have some question. how to receive data from another page's div? I am tried to do this: open a.php, send html data from div#send to b.php, then return the data from b.php div#aa and show in a.php div#result, from b.php div#bb and show in a.php div#text. Thanks.

a.php

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script> 
<script language="javascript"> 
$(document).ready(function () {      
var params = "value=" + $('#send').text();   
$.ajax({        
     url:'b.php',        
     type:'post',                 
     dataType:'html',            
     data:params,            
     success:data     
     }); 
function data (html) {   
$("#result").html('#aa'); 
$("#text").html('#bb'); 
} 
}); 
</script>
<body>
<div id="result"></div>// I need return 'this is an apple.'
<div id="text"></div>//I need return 'A red apple'.
<div id="send">apple</div>

b.php

<?php
echo '<div id="aa">';
echo 'This is an '.$_REQUEST['value'].'.'; 
echo '</div>';
echo '<div id="bb">';
echo 'A red '.$_REQUEST['value'].'.'; 
echo '</div>';
?>


You need to turn the response string into DOM elements, select those elements, and append them where you want.

function data (html) {
    var $html = $( html );             // create DOM elements in a jQuery object

    $html.filter('#aa').appendTo("#result"); // filter out the '#aa' element and
                                             //    append it to '#result'

    $html.filter('#bb').appendTo("#text");   // filter out the '#bb' element and
                                             //    append it to '#text'
}

I used the filter()[docs] method because the elements you're targeting appear to be at the top level of the HTML response.

If they were nested deeper, you'd use the find()[docs] method instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜