我正在使用 DOM 為我創建一個元素,顯示特定評論收到的點贊數量。當有人喜歡資料被推送到 API 的按鈕時。但是,為了看到喜歡的數量增加,我必須手動重新加載頁面。
有沒有辦法自動重繪 那個特定的 DOM 元素,這樣當用戶點擊喜歡按鈕時,喜歡的數量會自動增加(所以重繪 ),而無需手動重新加載網站?
// shows the amount of likes
const likeCounter = document.createElement("p");
likeCounter.classList.add("comments__container-content-like-counter");
functionsDiv.appendChild(likeCounter);
likeCounter.innerText = commentData.likes;
// likes a comment and adds it to the api
commentLike.addEventListener("click", () => {
const addLike = commentData.id;
axios.put(`${baseURL}comments/${addLike}/like?api_key=${apiKEY}`).then(() => {
console.log("Like has been added!");
});
});
uj5u.com熱心網友回復:
干得好:
// shows the amount of likes
const likeCounter = document.createElement("p");
likeCounter.classList.add("comments__container-content-like-counter");
functionsDiv.appendChild(likeCounter);
likeCounter.innerText = commentData.likes;
// likes a comment and adds it to the api
commentLike.addEventListener("click", () => {
const addLike = commentData.id;
axios.put(`${baseURL}comments/${addLike}/like?api_key=${apiKEY}`).then(() => {
console.log("Like has been added!");
likeCounter.textContent = Number(likeCounter.textContent) 1
});
});
只需添加行
likeCounter.textContent = Number(likeCounter.textContent) 1
到承諾鏈。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/510231.html
上一篇:將屬性添加到xml子標簽
