使用 JS 或 jQuery 更新数据-*

Sto*_*ace -1 html javascript jquery dom

有没有一种方法,以更新的值data-*HTML5的)元素divJavaScriptjQuery

syn*_*t1c 6

您可以使用jQuery.prototype.attr或者jQuery.prototype.data如果您使用 jQuery

$(selector).attr('data-something', 'stuff')

$(selector).data('something', 'stuff')

或者您可以使用datasetElement.prototype.setAttribute用于香草javascript

document.querySelector(selector).dataset['something'] = 'stuff' document.querySelector(selector).dataset.something = 'stuff' document.querySelector(selector).setAttribute('data-something') = 'stuff'

  • 支持展示“vanialla js”和“jQuery”方式 (2认同)