自定義一般屬性
通過getAttribute獲取對應的屬性
<ul>
<li product-id="1000">basketball</li>
<li product-id="1001">football</li>
<li product-id="1002">volleyball</li>
<li product-id="1003">tennis</li>
</ul>
var productList = [];
var products = document.getElementsByTagName("li");
for (var i = 0, len = products.length; i < len; i++) {
productList.push(products[i].getAttribute('product-id'));
}
console.log(productList)
自定義data屬性
通過getAttribute或者dataset獲取對應的屬性
<ul>
<li data-product-id="1000">basketball</li>
<li data-product-id="1001">football</li>
<li data-product-id="1002">volleyball</li>
<li data-product-id="1003">tennis</li>
</ul>
<script >
var productList = [];
var products = document.getElementsByTagName("li");
for (var i = 0, len = products.length; i < len; i++) {
// productList.push(products[i].getAttribute('data-product-id'));
productList.push(products[i].dataset.productId);
}
console.log(productList)
</script>