JQuery data selector not updating with .data

前端 未结 2 556
失恋的感觉
失恋的感觉 2020-12-01 10:05

Basically if I have a div loaded onto a page with a data-test attribute and change the value of it with jquery\'s .data(\'test\') I can no longer s

2条回答
  •  天命终不由人
    2020-12-01 10:42

    jQuery .data() is initially populated with values from the data- attributes, but setting it only stores the associated new value in memory. It doesn't change the attribute in the DOM. To change the attribute, you have to use:

    $('#one, #three').attr('data-test', 'changed');
    

    The docs are at http://api.jquery.com/jQuery.data/

提交回复
热议问题