问题描述
我有这样的链接:
<a class="" onclick="delete_image('imageidentifier', 1, 0);
return confirm('Are you sure you want to delete this image?');">Delete Image</a>
现在的问题是,如果有人单击取消,该图像将被删除。 我该如何预防?
1楼
dramzy
5
已采纳
2015-07-25 20:44:09
if (confirm('Are you sure you want to delete this image?')){
delete_image('imageidentifier', 1, 0);
}
但这不属于html,只需为其创建一个函数并在onclick=''
内调用它即可onclick=''
2楼
godban
0
2015-07-25 21:03:24
您应该在确认中使用delete_image函数,可以为此使用第三个函数:
<a class="" onclick="askDelete()">Delete Image</a>
function askDelete() {
if (confirm('Are you sure you want to delete this image?')) {
delete_image('imageidentifier', 1, 0);
}
}
但更好的方法是在.js内的此链接上添加事件:
<a class="foo">Delete Image</a>
document.querySelector('.foo').addEventListener('click', function () {
if (confirm('Are you sure you want to delete this image?')) {
delete_image('imageidentifier', 1, 0);
}
});