如何确定特定元素的所有数据属性的长度?

问题描述

HTML:

<div id="me" data-property-1="1" data-property-2="2" data-property-3="3"></div>

是否可以使用vanilla.js获取div的所有数据属性的长度?

解决方法

您能尝试一下吗?

const dataSetLength = Object.keys(document.getElementById('me').dataset).length;
console.log('Length of dataset:',dataSetLength);
<div id="me" data-property-1="1" data-property-2="2" data-property-3="3"></div>