2016-10-04 3 views
-1

data-*HTML5)要素の値をdivJavaScriptまたはjQueryに更新する方法はありますか?データをJSまたはjQueryで更新する

+3

'$(セレクタ).dataセクション( 'プレフィックス'、newValueに)' https://api.jquery.com/data/またはhttps://developer.mozilla.org/en -US/docs/Web/API/HTMLElement/dataset – Satpal

+1

https://api.jquery.com/jquery.data/ –

+1

@Satpal質問はしていますが、入力した両方のメソッドで属性値を更新しないでください –

答えて

3

あなたはjQueryの

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

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

それとも、JavaScriptの

バニラため datasetまたは Element.prototype.setAttributeを使用することができますを使用している場合は、 jQuery.prototype.attrまたは jQuery.prototype.dataを使用することができます

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

+0

upvote 'vanialla js'と' jQuery'を示すために – Stophface

0

シンプル

<div data-xyz="50"> 

$("div").data("xyz",30) 
関連する問題