【发布时间】:2020-05-02 17:44:38
【问题描述】:
如何从包含 html 内容的 JS 变量中提取特定的 html。
在下面的示例中,JS 变量包含从 Fetch API 请求返回的整个 html 内容。我只想从 content 类的部分中提取内容。
变量
const aVariable = fetch(...etc.. etc.. // the returned value)
返回值包含:
<section class="wrapper">
blah blah blah
<div>
blah blah
</div>
<section class="content">
It's the one that I want
<div>
Whoa whoa whoa yeah!
</div>
</section>
blah blah
</section>
我只想要content,但包括它的<section>。
【问题讨论】:
-
你引用了 jquery 吗?
-
@NithinChandran No. 试图避免它。这还有很多,jQuery 真的会减慢它的速度。这是一个备份,以防在返回的内容之间触发返回的提取。它是一种备份,但很重要。
-
使用
document.querySelector('.content').outerHTML有帮助吗? -
“包括它的
<section>”是什么意思。该部分内的所有内容? -
包容意味着他也需要section标签的标记
标签: javascript variables extract innerhtml