这是我的第一个问题。
如何使用正则表达式(或任何其他更好的方法)根据其中的文本匹配 div A 和 div B(两者具有相同的文本),然后删除匹配的 div B?
示例 HTML::
<div class="A"> Hello </div>
<div class="B"> Hello </div>
<div class="C"> Bye </div>
......... and so on.
最佳答案
有很多方法可以做到,一种方法是进行查找
(function (){
var lookup = {}; //store text here
$("div").each( function () { //loop through the elements
var elem = $(this); //reference to current
var txt = $.trim(elem.text()); //get its text
if (lookup[txt]) { //if we have had this text already, remove the element
elem.remove();
} else {
lookup[txt] = true; //set the key in the lookup
}
});
}());
关于javascript - jQuery/HTML 匹配 div 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25626135/