从AJAX请求获取页面内容后,我可以选择标签元素,但不能选择正文元素 [英] After getting a page's content from an AJAX request I can select tag element but cannot select body element
问题描述
我通过AJAX请求获得了html页面
I got an html page through an AJAX request
$.ajax({
async: true,
method: 'GET',
url: linkPage,
// cache: true,
success: function (data) {
console.log(data);
}
});
我得到的数据格式如下:
The data format I get like this:
<!DOCTYPE html>
<html>
<head>
...
</head>
<body id="sustainable" class='sustainable'>
<div id="wrap">
<main class="temp>
<section class="sec01">
...
</section>
</main>
</div>
</body>
</html>
现在,我想通过以下代码获取主体ID和类别( 可持续" ):
Now I want to get the body id and class (which is "sustainable") via this code:
$(data).find('body').attr('class');
但是我不知道为什么我不能得到它,它返回 undefiend .但是,当我通过此代码获取html内容或<main>
的类时:
But I have no idea why I can't get that, it returns undefiend. But when I get html content or class of <main>
by this code:
$(data).find('main').attr('class');
$(data).find('main').html();
它恰好返回我想要的.谁能解释我为什么?
It returns exactly what I want. Can anybody explain me why?
到目前为止,我已经通过创建这样的虚拟DOM尝试了一些解决方案,并且可以根据需要从AJAX数据中选择<body>
和<main>
:
I've tried some solutions so far by create a virtual DOM like this, and I can select <body>
and <main>
from AJAX data as I want: Cannot get body element from ajax response
但是我仍然想知道为什么我不能选择<body>
类和html作为第一种情况?
But I still wonder why I can't select <body>
class and html as first case?