javascript - 如何使用jquery获取变量中元素的文本?

标签 javascript jquery html html-parsing selector

var markup = '<div class="ExternalClass34E9F553C2F74AA2B6D693A07BA166AC">Employee Self-Service pages have been corrected but may require you to refresh the page.</div><div class="ExternalClass34E9F553C2F74AA2B6D693A07BA166AC">&#160;</div><div class="ExternalClass34E9F553C2F74AA2B6D693A07BA166AC">If the problem remains, follow <a href="/Shared%20Documents/EBS%20Page%20Refresh%20Instructions_rkc.pdf">these instructions</a>. &#160;</div>';           
var str = "";
$(markup).find("div[class^='ExternalClass']").each(function(){
    str += $(this).text();
})

如何抓取以 ExternalClass 开头的 markup 中所有 div 的内容?

最佳答案

$(markup) 选择器包含所有 ExternalClass 类,您不能使用 .find()因为它没有任何匹配的 child 。您需要使用 .filter()过滤选定的元素。

var markup = "<div...";
var str = "";
$(markup).filter("div[class^='ExternalClass']").each(function(){
    str += $(this).text();
})

var markup = '<div class="ExternalClass34E9F553C2F74AA2B6D693A07BA166AC">Employee Self-Service pages have been corrected but may require you to refresh the page.</div><div class="ExternalClass34E9F553C2F74AA2B6D693A07BA166AC">&#160;</div><div class="ExternalClass34E9F553C2F74AA2B6D693A07BA166AC">If the problem remains, follow <a href="/Shared%20Documents/EBS%20Page%20Refresh%20Instructions_rkc.pdf">these instructions</a>. &#160;</div>';
$(markup).filter("div[class^='ExternalClass']").each(function(){
    console.log($(this).text());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

关于javascript - 如何使用jquery获取变量中元素的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39961918/

相关文章:

html - 悬停改变边框颜色不均匀

javascript - 试图获取动态幻灯片文本。容器上没有固定高度。见 fiddle

javascript - 中止 Meteor.js 中的链接重定向

javascript - Cpanel 和 WHM 不会在 Chrome 中保存密码

javascript - 当多个测试文件时 Jest 不输出描述名称或测试名称

javascript - 如何显示动态添加内容的提示框?

javascript - 触发点击事件后触发函数

jQuery 在 Chrome 和 Firefox 中回发后无法工作

html - 如何取消文本对齐 : justify for Chinese text (or any text that doesn't use space characters)?

javascript - 如何让我的函数找到我的输入参数?