2009-07-04 61 views
1

我有一個PHP文件,該文件將返回類似jQuery的阿賈克斯 - 獲取元素的內部文本

<div id="title">Add Us On Myspace!</div><img id="image" src="http://i39.tinypic.com/67jybs.png" alt="Add Us On Myspace!" /> 

此使用這就是所謂的...

$.ajax({ 
    url: "Tabnews.php", 
    success: function(tab1html){ 
      $("#tabs-1").html(tab1html); 
    } 
}); 

我需要定義#的內容title和#image在jQuery中的一個變量,所以我可以寫在其他地方。

我該怎麼做?

回答

5

這應做到:

var $html = $(tab1html); // parse string into DOM structure 
var $title = $html.filter('#title'); // keep the title 
var $image = $html.filter('#image'); // keep the image 

// add to whatever elements we want... 
$('#my_element').append($title); 
$('#my_other_element').append($image); 
+0

爲什麼你的JavaScript變量有$前綴呢?我不會建議,除非你提倡一些匈牙利風格的符號。 – 2009-07-04 08:35:08