var markup = '<div class="ExternalClass34E9F553C2F74AA2B6D693A07BA166AC">Employee Self-Service pages have been corrected but may require you to refresh the page.</div><div class="ExternalClass34E9F553C2F74AA2B6D693A07BA166AC"> </div><div class="ExternalClass34E9F553C2F74AA2B6D693A07BA166AC">If the problem remains, follow <a href="/Shared%20Documents/EBS%20Page%20Refresh%20Instructions_rkc.pdf">these instructions</a>.  </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"> </div><div class="ExternalClass34E9F553C2F74AA2B6D693A07BA166AC">If the problem remains, follow <a href="/Shared%20Documents/EBS%20Page%20Refresh%20Instructions_rkc.pdf">these instructions</a>.  </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/