在数据响应中选择 #id.html

Select #id.html in data response

本文关键字:#id html 选择 数据 响应      更新时间:2023-09-26
function getJobs2(pars) {
    $.ajax({
        //alert(pars);
        url: 'lib/ajax/getJobs2.php',
        type: "POST",
        data: pars,
        success: function (data) {
            /* THIS line returns allways null*/
            console.log($(data).find('#home_right').html());
            /* I can see #home_right in the output!! */
            console.log(data);
            $('#home_right').html($(data).find('#home_right').html());
        }
    });
}

问题是:

$(data).find('#myDiv').html()没有返回我需要的 HTML...但为空。我可以在整个数据输出中获取所需的div。

我选错了吗?

我假设#home_right是响应中的顶级元素。然后使用.filter [文档]

$('#home_right').html($(data).filter('#home_right').html());

.find仅搜索所选元素的后代,而不搜索所选元素本身。

如果您发布回复,则更容易提供有用的答案。

尝试:

console.log($("<div class='dummy-wrapper'>" + data + "</div>").find("#home_right"));

演示