jQuery – data()用法
data()可以用在讀取標籤屬性「data-」內的值
若標籤的自訂屬性「data-」的值為JSON格式也能使用,如以下範例
<script>
$(function (){
//讀取一般的數據
var get_simple = $(".simple").data("setting");
console.log("get_simple : " + get_simple);
//讀取JSON數據
var get_json = $(".get_json").data("setting").a;
console.log("get_json : " + get_json);
})
</script>
<!– 一般用法 –>
<div class="simple" data-setting="word"></div>
<!– JSON格式 –>
<div class="get_json" data-setting='{"a":"on"}'></div>