更新时间:2022-06-03 03:20:57
对于.dataset
或data-
属性,这似乎是一个完美的用例. MDN 上有一个很棒的页面,显示了如何使用此功能.基本上,将uid以<data-uid = "ach782bckhbc23whatever">
的形式存储在HTML中的<tr>
或<button>
元素上,然后单击,使用evt.target.dataset.uid
或evt.target.parentElement.dataset.uid
获取该行的uid,以进行.get()
firebase调用. /p>
This appears to be a perfect use case for the .dataset
or data-
attribute. MDN has a great page showing how to use this. Basically, store the uid as <data-uid = "ach782bckhbc23whatever">
in your HTML on the <tr>
or the <button>
element an on click, use evt.target.dataset.uid
or evt.target.parentElement.dataset.uid
to get the row's uid to make your .get()
firebase call.
<div id="user" data-id="1234567890" data-user="johndoe" data-date-of-birth>John Doe</div>
let el = document.querySelector('#user');
// el.id == 'user'
// el.dataset.id === '1234567890'
// el.dataset.user === 'johndoe'
// el.dataset.dateOfBirth === ''
el.dataset.dateOfBirth = '1960-10-03'; // set the DOB.
// 'someDataAttr' in el.dataset === false
el.dataset.someDataAttr = 'mydata';
// 'someDataAttr' in el.dataset === true