jQuery的阿贾克斯 - 获取元素内部文本元素、文本、jQuery、阿贾克斯

2023-09-10 21:48:17 作者:一颗心为你り

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

I have a PHP file which will return something like

<div id="title">Add Us On Myspace!</div><img id="image" src="https://m.xsw88.com/allimgs/daicuo/20230910/1595.png" alt="Add Us On Myspace!" />

这是使用这种所谓的...

This is called using this...

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

我需要在jQuery的变量定义#title和#image的内容,这样我就可以写的东西他们在哪里。

I need to define the contents of #title and #image in a variable in jQuery so I can write them else where.

我怎样才能做到这一点?

How can I do this?

推荐答案

这应该做到这一点:

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