1 回答
TA贡献1780条经验 获得超1个赞
可能创建具有功能的按钮和内容段落document.createElement('el');并立即添加事件侦听器可能会更好。... 是这样的:
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
var userId = firebase.auth().currentUser.uid;
var urlRef = firebase.database().ref("/user/" + userId + "/note/");
urlRef.once("value", function (snapshot) {
snapshot.forEach(function (data) {
var subject = data.val().Subject;
var text = data.val().Note;
var date = data.val().Date;
// create button to contain subject and date
const button = document.createElement('button');
button.classList.add('collapsible');
button.style.fontWeight = 900;
button.innerHTML = `${subject} <span style="float: right;">${date}</span>`;
// create paragraph to contain content and hide it
const content = document.createElement('p');
content.textContent = text;
content.style.display = 'none';
// add button and content to notes
$('#my_notes').append(button);
$('#my_notes').append(content);
// add click event listener to button
button.addEventListener('click', (event) => {
event.target.classList.toggle('active');
const content = event.target.nextElementSibling;
if (content.style.display == 'block') {
content.style.display = 'none';
} else {
content.style.display = 'block';
}
});
});
});
}
});
添加回答
举报