我试图检查当前单击的文本是否与页面上的任何其他文本具有相同的数据,因为以下文本在页面上放置了两次。单击文本后,它将添加一个类(该位正在工作),如果它也具有与页面上任何其他文本相同的数据,那么该文本也将被赋予新类。
<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");
});
您的代码存在以下问题:
您缺少
.data()
的参数来指定数据名称。您在
if
条件两边缺少括号。您需要单独测试每个同级。与集合一起使用时,
.data()
仅返回集合第一个元素的值。您可以使用.each()
循环。
我没有使用循环,而是使用了与数据属性匹配的选择器。
关于javascript - 如果当前文本数据 = 任何其他文本数据那么...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17486324/