Remove multiple html5 data-attributes with jquery

后端 未结 7 1151
清酒与你
清酒与你 2020-12-16 14:25

So jquery api says the following:

Removing data from jQuery\'s internal .data() cache does not effect any HTML5 data- attributes in a document; use .

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-16 15:05

    // Fetch an array of all the data
    var data = $("a").data(),
        i;
    // Fetch all the key-names
    var keys = $.map(data , function(value, key) { return key; });
    // Loop through the keys, remove the attribute if the key contains "lorem".
    for(i = 0; i < keys.length; i++) {
        if (keys[i].indexOf('lorem') != -1) {
            $("a").removeAttr("data-" + keys[i]);
        }
    }
    

    Fiddle here: http://jsfiddle.net/Gpqh5/

提交回复
热议问题