【问题标题】:After getting a page's content from an AJAX request I can select tag element but cannot select body element从 AJAX 请求获取页面内容后,我可以选择标签元素但不能选择正文元素
【发布时间】:2020-01-07 07:28:25
【问题描述】:

我通过 AJAX 请求获得了一个 html 页面

$.ajax({
    async: true,
    method: 'GET',
    url: linkPage,
    // cache: true,
    success: function (data) {
        console.log(data);
    }
});

我得到的数据格式是这样的:

<!DOCTYPE html>
<html>
<head>
...
</head>
<body id="sustainable" class='sustainable'>
<div id="wrap">
    <main class="temp>
        <section class="sec01">
            ...
        </section>
    </main>
</div>
</body>
</html>

现在我想通过以下代码获取 body id 和类("sustainable"):

$(data).find('body').attr('class');

但我不知道为什么我不能得到它,它返回 undefiend。但是当我通过这段代码获得&lt;main&gt;的html内容或类时:

$(data).find('main').attr('class');
$(data).find('main').html();

它返回的正是我想要的。谁能解释一下为什么?

到目前为止,我已经通过创建这样的虚拟 DOM 尝试了一些解决方案,我可以根据需要从 AJAX 数据中选择 &lt;body&gt; 和 &lt;main&gt;:Cannot get body element from ajax response

但我仍然想知道为什么我不能选择&lt;body&gt; class 和 html 作为第一种情况?

【问题讨论】:

  • 您是否尝试过使用 document.getElementById("sustainable"); ?或者使用 JQuery $("#sustainable") 使用它的 id 或使用 $(".sustainable") 使用类名?

标签: javascript jquery ajax


【解决方案1】:

看起来,当给定一个这样的字符串时,jQuery 只会将 body 的内容 保存到它的集合中:

const data = `<!DOCTYPE html>
<html>
<head>
...
</head>
<body id="sustainable" class='sustainable'>
<div id="wrap">
    <main class="temp>
        <section class="sec01">
            ...
        </section>
    </main>
</div>
</body>
</html>`;
console.log($(data)[0]);
console.log($(data)[1]);
console.log($(data)[2]);
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;

(检查您的浏览器控制台。它选择了#wrap 和#wrap 本身周围的文本节点,但不是 &lt;head&gt; 或&lt;body&gt;)

您可以改用 DOMParser,它会尝试将 整个字符串 转换为文档,而不会尝试遗漏:

const data = `<!DOCTYPE html>
<html>
<head>
...
</head>
<body id="sustainable" class='sustainable'>
<div id="wrap">
    <main class="temp>
        <section class="sec01">
            ...
        </section>
    </main>
</div>
</body>
</html>`;
const doc = new DOMParser().parseFromString(data, 'text/html');
console.log(doc.body.className);
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;

使用 DOMParser 的另一个好处是,与 jQuery 不同,它不会在 HTML 字符串中执行可能不安全的代码:

const data = `<!DOCTYPE html>
<html>
<head>
...
</head>
<body id="sustainable" class='sustainable'>
<img src onerror="alert('evil')">
</body>
</html>`;
$(data);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
jQuery version, unsafe

const data = `<!DOCTYPE html>
<html>
<head>
...
</head>
<body id="sustainable" class='sustainable'>
<img src onerror="alert('evil')">
</body>
</html>`;
const doc = new DOMParser().parseFromString(data, 'text/html');
console.log(doc.body.className);
DOMParser version, safe

【讨论】:

    【解决方案2】:

    在 jQuery documentation 中解释了它无法按照您尝试的方式工作的原因:

    如果 HTML 比没有属性的单个标记更复杂,如上例所示,则元素的实际创建由浏览器的 .innerHTML 机制处理。在大多数情况下,jQuery 会创建一个新的 &lt;div&gt; 元素并将该元素的 innerHTML 属性设置为传入的 HTML sn-p。

    由于&lt;div&gt; 中不能有&lt;body&gt;,因此浏览器会忽略&lt;body&gt; 标记。

    文档接着说:

    在传递复杂的 HTML 时,某些浏览器可能无法生成完全复制所提供的 HTML 源的 DOM。如前所述,jQuery 使用浏览器的 .innerHTML 属性来解析传递的 HTML 并将其插入到当前文档中。在此过程中,某些浏览器会过滤掉某些元素,例如&lt;html&gt;、&lt;title&gt; 或&lt;head&gt; 元素。因此,插入的元素可能不代表传递的原始字符串。

    【讨论】:

    • 谢谢,我明白了。但就我而言,我也不能选择&lt;div id="wrap"&gt;。你知道为什么吗?
    • 过滤掉不能使用的元素后,那个&lt;div&gt;就成为集合中的顶级元素。 .find() 只搜索后代,它不匹配元素本身。
    【解决方案3】:

    以下 jQuery 不起作用:

    $(data).find('sustainable');
    

    由于 div 是顶级元素,而 data 不是元素而是字符串,要使其正常工作,您需要使用 .filter

    $(data).filter('sustainable.wrap');
    

    【讨论】:

    • 到目前为止,我已经尝试使用过滤器:$(data).filter('body').attr('class');,但它不起作用。
    猜你喜欢
    • 2013-05-18
    • 2023-03-29
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 2011-08-14
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多