jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – .addClass()其中data-id等于data-category?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在这里我构建了一个小提琴,我无法想到如何.addClass所点击的data-id等于数据类别.看看小提琴,你会明白得多.

Fiddle

在这里,我只是.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');

  });

这感觉有点蠢,但我没有搞砸这种写作(匹配数据属性)所以一点点知识将是惊人的.

答案::Sushanth –

$(".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');
});

解决方法

fiddle

var items = $('.item');
$(".breadcrumb-cell .breadcrumb").click(function () {
    var thEID  = $(this).data("id");

    items.filter(function() {
        return $(this).data('category') === thEID;
    }).addClass('active');

});

您可以使用filter方法.如果您要在其他地方使用项目以及此操作,这将非常有用.

大佬总结

以上是大佬教程为你收集整理的jquery – .addClass()其中data-id等于data-category?全部内容,希望文章能够帮你解决jquery – .addClass()其中data-id等于data-category?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。