课后作业,其实和上一篇的兼容element.children类似的实现,代码如下:
var someElement = {
dataset: function(element) {
if (element.dataset) {
return element.dataset;
// console.log(element.dataset);
} else {
var obj = {};
var list = element.attributes.length;
for (var i = 0; i < list; i++) {
var key = element.attributes[i].nodeName;
var value = element.attributes[i].nodeValue;
if ((/^data-\w+$/).test(key)) {
obj[key] = value;
}
}
return obj;
}
}
};