gpt4 book ai didi

javascript - 在 Ajax xhr 响应上使用 jQuery.filter 和 jQuery.find 时的不同结果

转载 作者:太空宇宙 更新时间:2023-11-04 11:40:22 25 4
gpt4 key购买 nike

我网站的一部分有一个 ajax 菜单。此菜单发送 GET 请求以检索单击链接的页面内容。我从请求中返回的内容是一个完整的 HTML 页面,其中包含页面上的所有元素,包括脚本和样式标签。我在 XHR 响应上使用 jQuery.filter 将脚本作为 HTML 脚本标记获取。但是当我尝试获取样式表链接标记时,它不起作用。如果我在 XHR 上使用 jQuery.finding,它只会给我样式链接标签。这是我使用的 Ajax

$.ajax({
type: 'GET',
url: url,
dataType: "html",
async: false,
error: function(oXHR, type, exception) {
console.dir(exception);
},
success: function(xhr) {
var scripts = $(xhr).filter('.vod'),
styles = $(xhr).find('link[name="vod"]');
$(styles).each(function() {
if (this && this.href) {
loadStyle(this);
}
});
$(scripts).each(function() {
if (this.text) {
$.globalEval(this.text || this.textContent || this.innerHTML || '');
} else if (this.src || this.href) {
loadScript(this);
}
});
$('#container').html($(xhr).find('#content')[0]);
}
});

最佳答案

Jquery 从响应中去除 html、head 和 body 标签。这是因为在设置 t adiv 元素的 innerHTML 时 JavaScript 的工作方式。所以你没有一个完整的 html 页面,你有 body 和 head 标签的子节点。

所以你需要使用过滤器,因为链接元素不是子元素。

你期待什么:[htmlNodeReference]

你得到什么 [title, link, h1, div, etc]

关于javascript - 在 Ajax xhr 响应上使用 jQuery.filter 和 jQuery.find 时的不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31384856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com