2009-07-04 27 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