Filter element based on .data() key/value

前端 未结 5 1478
庸人自扰
庸人自扰 2020-12-12 13:08

Say I have 4 div elements with class .navlink, which, when clicked, use .data() to set a key called \'selected\', to a value of

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-12 13:56

    We can make a plugin pretty easily:

    $.fn.filterData = function(key, value) {
        return this.filter(function() {
            return $(this).data(key) == value;
        });
    };
    

    Usage (checking a radio button):

    $('input[name=location_id]').filterData('my-data','data-val').prop('checked',true);
    

提交回复
热议问题