element.dataset in Internet Explorer

后端 未结 4 755
陌清茗
陌清茗 2021-01-06 14:45

I need a way to list the data-* attributes of an element. I would use Object.keys(element.dataset) but IE 9.0 doesn\'t have dataset s

4条回答
  •  暖寄归人
    2021-01-06 15:11

    element.attributes will give you a NamedNodeList with all attributes of the element.
    Just check the attribute names if they start with data-

    var attributes = element.attributes,
        i = attributes.length;
    
    for (; i--; ){
        if (/^data-.*/.test(attributes[i].name)) {
            console.log(attributes[i].name);
        }
    }
    

    ​Example

提交回复
热议问题