目前,我的HTML代碼示例如下所示:如何更改的屬性值裏面的數據屬性
<div class="jbar" data-init="jbar" data-jbar='{
"message" : "Test",
"state" : "open"
}'></div>
我怎樣才能改變這種情況下的state
價值closed
?
目前,我的HTML代碼示例如下所示:如何更改的屬性值裏面的數據屬性
<div class="jbar" data-init="jbar" data-jbar='{
"message" : "Test",
"state" : "open"
}'></div>
我怎樣才能改變這種情況下的state
價值closed
?
jQuery的會看起來像
var t=JSON.parse($('.jbar').attr('data-jbar'));
//or JSON.parse($('.jbar').data('jbar'));
t.state='closed';
$('.jbar').attr('data-jbar',JSON.stringify(t));
//or .data('jbar',JSON.stringify(t));
你好,我試過你的解決方案,但它仍然顯示'open'。這裏是小提琴:http://jsfiddle.net/bb9vnzw2/ –
@VuiLaChinh你的小提琴沒有'$('。jbar')。attr('data-jbar',JSON.stringify(t)); '部分。 –
這裏是一個小提琴設置'attr' http://jsfiddle.net/bb9vnzw2/3/像@Jason_P提到 – depperm
好,存儲,因爲它是,它是一個字符串,而不是一個對象。你必須解析它,然後改變它,然後再存儲它。 – Draco18s
謝謝你的幫助。如果你能給我一個例子說明如何去做,或者有什麼更簡單的方法來實現我的目標,那該多好? –