从大字符串中获取文字



我已经使用ajax获取了整个网站html的大部分文本。现在,我需要在本文中抓住特定的div。如何抓住DIV并仅显示其文字?我已经尝试了.match和.search方法,只是无法理解它的真正工作方式。我还在Localhost中使用XAMPP,Windows10。

您可以将html字符串传递到jQuery(),使用.find()获取具有特定属性的元素,链.text()以获取匹配的元素的.textContent

var htmlStr = `<div>text<div class="specific">text</div></div>`;
    
var text = $(htmlStr).find(".specific").text();
console.log(text);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>

最新更新