我正在使用 bootstrap,(好吧,我是新手),我发现了这两个属性,有人可以给我解释一下吗?
最佳答案
只是为了发扬@Larsenal 的观点,自定义数据属性对于开发人员来说可能非常方便。正如规范所说:
Custom data attributes are intended to store custom data private to the page or application, for which there are no more appropriate attributes or elements. These attributes are not intended for use by software that is independent of the site that uses the attributes.
示例用法包括:
存储初始高度/宽度,稍后可能会被 JS 更改。
通过 JavaScript 获取和设置这些属性的方法很简单 - 使用 getAttribute
和 setAttribute
。
<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'
</script>
记住,这不是 好的做法
。因此,请使用数据集属性
。
在这里阅读更多关于数据属性的信息:http://html5doctor.com/html5-custom-data-attributes/
作为 JavaScript 开发人员,您会爱上它们(也可能不会)。
关于html - 什么是 data-target 和 data-slide-to 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19846686/