jquery 筆記

常用軟體,程式語言的筆記和疑問
Forum rules
盡量做到:
每一個貼子都精心編輯;
每一個疑問都得到解答。
ejsoon
Site Admin
Posts: 3919
Joined: 2016 Jan 10, 22:15

Re: jquery.data()瞭解一下

Post by ejsoon » 2018 Nov 20, 11:34

這裡

比.attr()少寫一個data-,數據以對象保存,而不是.attr()以字符串保存。

以下代碼說明它們走兩條不同的路綫:

Code: Select all

$('#foo').data('foo', 'baz');

console.log( $('#foo').attr('data-foo') );
//outputs "bar" as the attribute was never changed

console.log( $('#foo').data('foo') );
//outputs "baz" as the value has been updated on the object

Post Reply