3

ここで私はフィドルを作成し.addClassました。クリックされたデータIDがデータカテゴリと等しい場所を考えることはできません。フィドルを見てください。もっとよく理解できます。

フィドル

ここでは.addClass、すべての.itemクラスについて説明しますが、data-id に一致するデータ カテゴリにクラスを追加するように記述する方法がわかりません。

未完成の jQuery スニペット:

  $(".breadcrumb-cell .breadcrumb").click(function () {
      var theID  = $(this).data("id"),
          theCAT = $('.item').attr("data-category"),
          item   = $('.item')

      //This just shows you that when you click the element it returns the data-id each click
      alert(theID);

      // Here I gave it a few shots but no success, so I just add .active to all
      item.addClass('active');

  });

これはちょっとばかげているように感じますが、私はこの種の書き込み (データ属性の一致) をいじっていないので、知識の少しの隆起は途方もないでしょう.

回答: by: スシャント --

$(".breadcrumb-cell .breadcrumb").click(function () {
var theID = $(this).data("id"),
    $allItem = $('.item'),
    $currItem = $('.item[data-category=' + theID + ']')

$currItem.addClass('active');
$allItem.not($currItem).removeClass('active');
});
4

2 に答える 2

6

このセレクターを使用できます

  $('.item[data-category=' + theID + ']').addClass('active');

このセレクターは、特定のdata-category

コード

$(".breadcrumb-cell .breadcrumb").click(function () {
    // This will have the category
    var theID = $(this).data("id"),
    //  All items
        $allItem = $('.item');

    // Current item is should be active
    var $currItem = $('.item[data-category=' + theID + ']');
    $('.item[data-category=' + theID + ']').addClass('active');
    // Remove the active class for other items
    $allItem.not($currItem).removeClass('active');
});

デモを確認

于 2013-08-08T21:56:47.090 に答える