jQuery的阿贾克斯 - 获取元素的内部文本(jQuery Ajax - Get Elements

2019-10-16 18:28发布

我有一个PHP文件,该文件将返回类似

<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);
    }
});

我需要定义在jQuery的一个变量#title和#image的内容,所以我可以将它们写别的地方。

我怎样才能做到这一点?

Answer 1:

这应该这样做:

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);


文章来源: jQuery Ajax - Get Elements Inner Text