2013-07-05 28 views
1

我想檢查當前點擊的文本是否與頁面上的任何其他文本具有相同的數據,因爲下面的文本會放在頁面上兩次。一旦文本被點擊,它將添加一個類(這個位正在工作),並且如果它也具有與頁面上任何其他文本相同的數據,那麼該文本也將被賦予新類。如果當前文本數據=任何其他文本數據,然後...?

<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"); 

    }); 

回答

0
$(".AJAXPagerSpan").click(function() { 
    $(".highlightclass").removeClass("highlightclass"); 
    $("span[data-num=" + $(this).data("num") + "]").addClass("highlightclass"); 
}); 

你的代碼中存在以下問題:

  1. 你失蹤的參數.data()指定數據的名稱。

  2. 您錯過了if條件的括號。

  3. 您需要分別測試每個兄弟。與集合一起使用時,.data()僅返回集合第一個元素的值。您可以使用.each()循環。

我使用了一個匹配數據屬性的選擇器,而不是循環。

FIDDLE

+0

這似乎沒有工作:( –

+0

沒有你的元素具有相同的數據。 – Barmar

+0

它的工作原理,看到小提琴。 – Barmar

相關問題