【问题标题】:jquery - how to show elements with data attribute equal to value of arrayjquery - 如何显示数据属性等于数组值的元素
【发布时间】:2023-03-17 18:23:01
【问题描述】:

我正在尝试创建一个过滤器插件, 在初始化和其他函数之后,我得到了两个数组

foo1=[test,test2,test3]
foo2=[test,test3]

现在我必须显示具有 tin foo2 的数据属性的元素,该元素等于 foo1 中的单击元素数据属性

这是我的代码:

r$.each($FILTER.foo2elem, function() {
var elemfoo2 = $(this).data();
var elemfoo2data = $(this).data().foo2elem.split(' ');

r$.each($FILTER.foo1saved, function(i, selectedFilters) {
    if ($.inArray(selectedFilters, elemfoo2data) !== -1) {
        console.log(selectedFilters + ' è uguale a ' + elemfoo2data);
        console.log('show element with foo2 data equal to foo1');
        //how can i hook that element
    } else {
        console.log('hide');
        //how hide other element
    }
})

});

请帮帮我,我的英语很抱歉

【问题讨论】:

    标签: jquery arrays custom-data-attribute


    【解决方案1】:

    尝试过滤函数,如果在另一个数组中找到该对象,则返回项目。我喜欢使用它,因为它可以与任何对象进行比较。祝你好运。

    $(function() {
      var foo1 = ['one', 'two', 'three'];
      var bar1 = ['one', 'three'];
    
      var rr = foo1.filter(function(index) {
        return jQuery.inArray(index, bar1) != -1;
      });
    
      console.log(rr);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    【讨论】:

      猜你喜欢
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      • 2017-08-04
      相关资源
      最近更新 更多