简体   繁体   English

在单个元素上查找所有数据属性

[英]Find All Data Attributes on a Single Element

Anyone know a quick and efficient way to grab all the data attributes from a single element? 任何人都知道从单个元素中获取所有数据属性的快速有效方法吗? I realize that jQuerys .data() will do just that, however it will not give me data attributes set using .attr() UNLESS I first select the data attribute using .data(); 我意识到jQuerys .data()会做到这一点,但它不会给我使用.attr()设置数据属性。除非我首先使用.data()选择数据属性; Also, you can't select elements by data attributes that were added using .data(), which seems silly. 此外,您无法通过使用.data()添加的数据属性选择元素,这看起来很愚蠢。

html HTML

<div data-foo="bar"></div>

javascript JavaScript的

$("div").data();
//returns {foo:bar} good :)

$("div").attr("data-hello","world");
$("div").data()
//returns {foo:bar} no good :(

$("div[data-hello]");
//returns the div, all good :)

$("div").data("find","me");
$("div[data-find]");
//returns nothing, very bad

Hopefully this explains 希望这能解释

You can use the dataset property in modern browsers(IE11+ only), but you can enhance the solution to use .attributes to support older browsers 您可以在现代浏览器中使用dataset属性(仅限IE11 +),但您可以增强解决方案以使用.attributes来支持旧版浏览器

var $in = $('input'),
    input = $in[0], //here input is a dom element reference
    dataMap = input.dataset;
//if dataset is not supported
if (typeof dataMap == 'undefined') {
    dataMap = {};
    $.each(input.attributes, function (key, attr) {
        var match = attr.name.match(/^data-(.+)/);
        if (match) {
            dataMap[match[0]] = attr.value;
        }
    })
}
$.each(dataMap, function (key, value) {
    console.log(key, value)
})

Demo: Fiddle 演示: 小提琴

Different versions of Internet Explorer support different features that are relevant to this issue. 不同版本的Internet Explorer支持与此问题相关的不同功能。 In version 11, support for dataset was added, which returns a DOMStringMap of data-attribute names (minus the "data-" portion), and their respective values. 在版本11中,添加了对dataset支持,该dataset返回数据属性名称的DOMStringMap (减去“数据 - ”部分)及其各自的值。

In versions 9 and 10, we can leverage Array.prototype.slice to convert the well-supported attributes collection into an array that we can then reduce to an object, similar to DOMStringMap . 在版本9和10中,我们可以利用Array.prototype.slice将支持良好的attributes集合转换为一个数组,然后我们可以将其缩减为一个对象,类似于DOMStringMap

We can combine both of these approaches into a single function that accepts an element as its argument, and returns an object like this { name: "pat", "age": 23 } for all data- attributes: 我们可以将这两种方法结合到一个接受元素作为参数的函数中,并为所有数据属性返回一个像{name:“pat”,“age”:23}这样的对象:

function getDataAttributes ( el ) {
    return el.dataset || [].slice.call( el.attributes ).reduce(function ( o, a ) {
        return /^data-/.test( a.name ) && ( o[ a.name.substr( 5 ) ] = a.value ), o;
    }, {} );
}

If you require support for Internet Explorer 8, or below, you can still use the above approaches, and simply polyfill Array.prototype.reduce . 如果您需要Internet Explorer 8或更低版本的支持,您仍然可以使用上述方法,只需填充Array.prototype.reduce

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM