The data method in jQuery can only get and cannot set values?

Time:2021-11-20

The data method in jQuery can only get and cannot set values?



<div id="div1" data-test="test">1</div>
<script>
    console.log($("#div1").data("test"));//test
</script>

The data method does store or read data on the element
In fact, this storage does not change the data attribute of the element, but sets a value for the object in the cache, which will be obtained directly in future retrievalcacheA value in.

terms of settlement:

To set the value of the data attribute on the element, useattr()Method!!!