jQuery .ajax() html 响应。查找<正文>

标签 jquery html ajax

我有一个 .ajax 调用,它通过 post 请求加载 url,就像 .load() 所做的那样。我正在读取responseText 以获取有效的HTML。然后我想获取 , 和我自己的#content id。

标题用于替换页面标题。 #content 是页面上要替换的内容,我需要 class 属性的 body 标记,因为它们是我要加载的每个页面的特定类。

    $.ajax({
    url: url,
    type: 'POST',
    data: {},
    complete: function (result) {

        var html = $('<div>' + result.responseText + '</div>'),
            title = html.filter('title:first').text(),
            b = html.find('body'),
            inner = html.find('#content').html();

        console.log(b);

        //change document title
        document.title = title;

        //add new content
        wrap.html(inner);

        //for home page
        $(window).trigger('resize');

        //fade in
        wrap.fadeIn(500, function () {

            wrap.trigger('newpage');
            $('#wrap').css('min-height', '100%');

        });

    }
});

假设定义了换行。请。这里的问题是,我无法检索正文来阅读类(class)。它总是返回未定义。我也尝试过使用 .filter() 而不是 .find(),但没有成功。

有什么想法吗?

最佳答案

每个文档只能有一个<body>标签,通常浏览器会将这些标签从 html 中剥离。

您只需在 Chrome 调试器中输入以下内容即可对此进行测试:

$('<div><head></head><body>Example Text</body></div>');

将返回:

[<div>Example Text</div>]

这记录在 JQuery API

When passing in complex HTML, some browsers may not generate a DOM that exactly replicates the HTML source provided. As mentioned, jQuery uses the browser"s .innerHTML property to parse the passed HTML and insert it into the current document. During this process, some browsers filter out certain elements such as <html>, <title>, or <head> elements. As a result, the elements inserted may not be representative of the original string passed.

关于jQuery .ajax() html 响应。查找<正文>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18259151/

相关文章:

jquery - 删除链接后的内容

javascript - 在 jQuery 循环内获取以某个字符串开头的 id

c# - Microsoft JScript 运行时错误 : Sys. WebForms.PageRequestManagerServerErrorException:从服务器返回的状态代码为:500

php - 如何使用 PHP 单击文件名下载文件?

javascript - 全局范围、返回值和 ajax 的问题?

c# - ASP.Net Ajax 照片上传

php - 如何使用 php 发送服务器错误响应?

javascript - html <head> 无法正常工作

jquery - 选择具有指定 css 规则集的 dom 元素

javascript - 检查是否有多个具有相同值的输入字段