我有一个 .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/