所以我使用 JavaScript 动态生成 HTML 代码,这些代码加载到我的 Firebase 实时数据库中的所有图像中。我目前正在实现一个附加到每个图像的按钮,单击该按钮将删除该图像。但是,在使用标准 JavaScript 和 Jquery 多次尝试获取此 div 的 ID 属性后,警报框中的 id 始终为“未定义”。检查网页让我看到图像的 id 总是加载得很好,所以我知道它在那里。
JavaScript 函数来响应我的 html 'onclick 事件'
function deleteFile(){
var postId = $(this).closest('div').attr('id');
alert("You have selected id: " + postId);
var sure = confirm("Are you sure you want to delete this post?");
if(sure){
firebase.database().ref().child('Posts/' + postId).remove().then(function(){
alert("Post deleted sucessfully");
});
}
};
附加的图像是在实际的 chrome 检查器上生成的 html。ID当然都是唯一的。
白猪掌柜的
繁星淼淼
相关分类