javascript - 未捕获的类型错误:deleteButton 不是 HTMLButtonElement.onclick 处的函数
问题描述
我需要在我的卡片上添加一个删除按钮,这就是我得到的错误。
解决方案
这就是它被要求完成的方式。我发现它超出了我的知识范围,但如果有人是古玩,我想我可以分享它。
const commentsUrl = "http://localhost:8080/comments";
let comments = [];
function getComments() {
fetch(commentsUrl, { method: "GET" })
.then((res) => res.json())
.then((data) => {
comments = data;
renderComments(comments);
});
}
const renderComments = (comments) => {
const commentsContainer = document.getElementById("content");
commentsContainer.innerHTML = "";
comments.forEach(function (post) {
const comment = document.createElement("div");
comment.innerHTML = `
<div id=${post.id} class="card">
<div class="card text-center bg-info" style="width: 18rem;">
<div class="card-body ">
<h5 class="card-user ">${post.user}</h5>
<h6 class="card-id mb-2 text-muted">Id: ${post.id}</h6>
<p class="card-content">"${post.content}" </p>
<p class="card-date">${post.date}<p>
<button type="button" class="btn btn-primary btn-sm">Edit</button>
<button type="button" class="btn btn-danger btn-sm" id="deleteBtn" onclick='remove(${post.id})'>Delete</button>
</div>
</div>
</div>`;
commentsContainer.append(comment);
});
};
function remove(id) {
fetch(`${commentsUrl}/${id}`, { method: "DELETE" }).then((comment) => {
const index = comments.findIndex(
(currentComment) => currentComment.id === comment.id
);
comments.splice(index - 1, 1);
renderComments(comments);
});
}
getComments();
推荐阅读
- flutter - 如何将两个音频文件与 just_audio 合并以同时播放?
- cognos - cognos 中的 Gateway URI 和 Dispatcher URI 有什么区别
- macos - 在 macOS 上本地使用 TCP 套接字时出现异常超时
- python - 如何在 Tensorflow 中扩充文本数据集?
- reactjs - 如果路径包含“?”,如何添加路由?
- java - 跨多个实体使用相同 UUID 的策略
- google-api - Google Rest API 和 google 登录
- python - Pyttsx3 不读取其他语言的文本
- ios - AVPlayer 无法播放 AVURLAsset 加载的 mp3 格式文件,其中选项 AVURLAssetPreferPreciseDurationAndTimingKey 在 iOS 14 上设置为 true
- reactjs - 如何将我的过滤器选项重置为其初始空白状态?反应