【问题标题】:How can i geht the lenght of all data-attributes of an specific elements?如何获取特定元素的所有数据属性的长度?
【发布时间】:2020-08-28 17:04:40
【问题描述】:

HTML:

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

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

【问题讨论】:

    标签: javascript custom-data-attribute string-length


    【解决方案1】:

    你可以试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-16
      • 1970-01-01
      • 2014-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-17
      相关资源
      最近更新 更多