html中data-href属性的用法?

如题所述

第1个回答  2017-12-29
html元素里面貌似没有这个属性,这个应该是自定义的属性, 自定义属性的使用方法:

<div id='strawberry-plant' data-fruit='12'></div>
<script>
// 'Getting' data-attributes using getAttribute
var plant = document.getElementById('strawberry-plant');
var fruitCount = plant.getAttribute('data-fruit');// fruitCount = '12'

// 'Setting' data-attributes using setAttribute
plant.setAttribute('data-fruit','7');// Pesky birds

</script>本回答被网友采纳
相似回答