如何更改数据属性中 属性 的值

How to change value of property inside data attribute

目前,我的示例 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));