获得“课程"来自< body>的attr $ .get()对象的 [英] Get "class" attr from <body> of $.get() object

查看:61
本文介绍了获得“课程"来自< body>的attr $ .get()对象的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从使用$ .get()检索的页面对象的body元素中获取class属性.在下面的示例中,将fullContent设置为对象,但是我无法选择body元素……我宁愿不使用文本操作来获取此信息

I need to get the class attribute from the body element of a page object retrieved with $.get(). In my example below, fullContent is set to the object but I can't select the body element... I'd rather not resort to text manipulation to get this

$.get(url, refreshFullContent);

var refreshFullContent = function(response, status, xhr)
{
    fullContent = response;   
    var bodyclass = $(fullContent).find("body").attr("class");
    $("body").attr("class", bodyclass);
}

有没有一种方法可以通过对象上的选择器来做到这一点(就像我在本示例中尝试做的那样)?如果不是,从文本中获取此内容的最佳方法是什么? xhr.responseText包含整个响应的字符串.

Is there a way to get this with selectors on the object (like I'm trying to do in this example)? If not, what is the best way to get this from the text? xhr.responseText contains the string of the entire response.

推荐答案

如果您收到完整的HTML文档响应,则浏览器之间的行为将不一致.

If you're getting a full HTML document response, you won't get consistent behavior between browsers.

某些浏览器会删除headbody元素.

Some browsers will strip away the head and body elements.

您可以尝试此操作,但不能保证:

You could try this, but no guarantees:

var fullContent = '<div>' + response + '</div>';
var bodyclass = $(fullContent).find("body").attr("class");
$("body").attr("class", bodyclass);

这篇关于获得“课程"来自&lt; body&gt;的attr $ .get()对象的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆