如果当前文本数据=任何其他文本数据,则.

If current text data = any other text data then...?

本文关键字:文本 数据 其他 任何 如果      更新时间:2023-09-26

当下面的文本被放在页面上两次时,我试图检查当前单击的文本是否与页面上的任何其他文本具有相同的数据。一旦点击文本,它将添加一个类(这个位正在工作),如果它也与页面上的任何其他文本具有相同的数据,那么该文本也将被赋予新的类。

<span class="AJAXPagerSpan" data-num="6">6</span>
<span class="AJAXPagerSpan" data-num="12">12</span>
<span class="AJAXPagerSpan" data-num="24">24</span>  

  $(".AJAXPagerSpan").click(function() {
        <%= this.ID %>_Pager.ChangeResultsPerPage($(this).html()); 

       // Check if any of its siblings are part of the "highlightclass" and remove them from it
        $(this).siblings().removeClass("highlightclass");
        if ($(this).data() == $(this).siblings().data()) {
        $(this).addClass("highlightclass");
        };
       // Add it to the highlightclass
        $(this).addClass("highlightclass");
  });
$(".AJAXPagerSpan").click(function () {
    $(".highlightclass").removeClass("highlightclass");
    $("span[data-num=" + $(this).data("num") + "]").addClass("highlightclass");
});

您的代码有以下问题:

  1. 您缺少用于指定数据名称的.data()参数。

  2. if条件周围缺少括号。

  3. 你需要分别测试每个兄弟姐妹。与集合一起使用时,.data()只返回集合的第一个元素的值。您本可以使用.each()循环。

我使用了一个与数据属性匹配的选择器,而不是循环。

FIDDLE