📅  最后修改于: 2022-03-11 15:02:10.140000             🧑  作者: Mango
const el = document.querySelector('#user');
// el.id === 'user'
// el.dataset.id === '1234567890'
// el.dataset.user === 'johndoe'
// el.dataset.dateOfBirth === ''
// set a data attribute
el.dataset.dateOfBirth = '1960-10-03';
// Result: el.dataset.dateOfBirth === '1960-10-03'
delete el.dataset.dateOfBirth;
// Result: el.dataset.dateOfBirth === undefined
if ('someDataAttr' in el.dataset === false) {
el.dataset.someDataAttr = 'mydata';
// Result: 'someDataAttr' in el.dataset === true
}