Do HTML5 custom data attributes “work” in IE 6?

后端 未结 6 1869
小鲜肉
小鲜肉 2020-11-22 06:35

Custom data attributes: http://dev.w3.org/html5/spec/Overview.html#embedding-custom-non-visible-data

When I say “work”, I mean, if I’ve got HTML like this:



        
6条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 06:57

    If you wanted to retrieve all of the custom data attributes at once like the dataset property in newer browsers, you could do the following. This is what I did and works great for me in ie7+.

    function getDataSet(node) {
        var dataset = {};
        var attrs = node.attributes;
        for (var i = 0; i < attrs.length; i++) {
            var attr = attrs.item(i);
            // make sure it is a data attribute
            if(attr.nodeName.match(new RegExp(/^data-/))) {
                // remove the 'data-' from the string 
                dataset[attr.nodeName.replace(new RegExp('^data-'), '')] = attr.nodeValue;
            }
        }
        return dataset;
    }
    

提交回复
热议问题