【问题标题】:Target all selectors where attr is equal to value from Array定位 attr 等于 Array 中的值的所有选择器
【发布时间】:2015-01-20 15:03:54
【问题描述】:

我需要淡出所有 id 等于值的 li,我该怎么做? Atm 它会影响所有 li,而不仅仅是具有匹配 id 的那些。

var cookie = jQuery.cookie('wishlist_cookie');
var parse = jQuery.parseJSON(cookie);
jQuery.each(parse['itemlist'], function (index, value) {
    var id = value['wishlist_item_id'];
    jQuery('li').attr('id', id).fadeOut();
});

parse['itemlist'] 返回

[Object wishlist_item_id: "227482" __proto__: Object ]

【问题讨论】:

    标签: jquery arrays cookies each


    【解决方案1】:

    呃……jQuery('#'+id).fadeOut()

    或者(应该更快):

    var cookie = jQuery.cookie('wishlist_cookie'),
        parse = jQuery.parseJSON(cookie),
        ids = jQuery.map(parse['itemlist'], function (index, value) {
            return '#' + value['wishlist_item_id'];
        });
    
    jQuery(ids.join(',')).fadeOut();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多