Remove multiple html5 data-attributes with jquery

后端 未结 7 1139
清酒与你
清酒与你 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 14:52

    Vanilla JS below clears all data attributes. If you want to add some if logic to remove a subset of data attributes, it should be trivial to add that functionality to the below JavaScript.

    function clearDataAttributes(el){
        if (el.hasAttributes()) {
            var attrs = el.attributes;
            var thisAttributeString = "";
            for(var i = attrs.length - 1; i >= 0; i--) {
                thisAttributeString = attrs[i].name + "-" + attrs[i].value;
                el.removeAttribute(thisAttributeString);
            }
        }
    }
    

提交回复
热议问题