javascript - jQuery/HTML 匹配 div 的文本

标签 javascript jquery html css regex

这是我的第一个问题。

如何使用正则表达式(或任何其他更好的方法)根据其中的文本匹配 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
        }
    });
}());

JSFiddle

关于javascript - jQuery/HTML 匹配 div 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25626135/

相关文章:

javascript - 如果重新加载页面,鼠标手形光标会在 Google Chrome 中消失

javascript - Karma + Jasmine - 找不到变量要求

javascript - 如何在 Javascript 中获取每个月的指定日期?

由于 adblock,Javascript Ajax 请求在 chrome 中失败,我有什么选择?

javascript - div 和 p 标签 float 很奇怪而且不正确

html - 为要显示的图像创建一个流畅的 3 列布局

javascript - 如何在html中指定一个本地文件

c# - 在 C# 环境下使用 Javascript 的建议

javascript - 在JS中更改警告框的标题

javascript - "Uncaught ReferenceError: jquery is not defined"- 向下滚动菜单