element.dataset在Internet Explorer中 [英] element.dataset in Internet Explorer

查看:141
本文介绍了element.dataset在Internet Explorer中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来列出元素的数据 - * 属性。我将使用 Object.keys(element.dataset),但IE 9.0没有数据集支持。我应该如何以这种方式对IE 9.0(和Chrome,Firefox,Safari)进行操作?

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 support. How should I do this in a way that works for IE 9.0 (and Chrome, Firefox, Safari)?

推荐答案

element.attributes 将给你一个 NamedNodeList 与元素的所有属性。

只需检查属性名称如果他们从数据开始 -

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);
    }
}

示例

这篇关于element.dataset在Internet Explorer中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆