data屬性(The Data Attribute)
阿新 • • 發佈:2017-11-20
das ext -s inline content col luci borde ucid
HTML片段
<div id="myDiv" data-custom-attr="My Value"> 巴拉巴拉,lady 嘎嘎 </div>
檢索自定義屬性的價值
var theDiv = document.getElementById(‘myDiv‘);
var attr = theDiv.getAttribute(‘data-custom-attr‘);
alert(attr); // My Value
此屬性還可以用在CSS中,例如下面這個有些傻裏傻氣的CSS文字改變的例子:
CSS代碼: .data_custom { display:inline-block; position: relative; } .data_custom:hover { color: transparent; } .data_custom:hover:after { content: attr(data-hover-response); color: black; position: absolute; left: 0; }
HTML代碼: <a class="data_custom" data-hover-response="我說過不要碰我!" href="#">不要碰我,雅蠛蝶~~</a>
data屬性(The Data Attribute)