...我想在用户想要删除帖子时询问用户,(同意或不同意)如果选择同意(ok),那么帖子将被删除,如果选择不同意(取消)对帖子不做任何事情
为了达到所需的结果,您需要更改一些 html 以传递当前元素(请参阅:this 关键字),并且像示例部分中报告的那样,您可以将一个名为showAlert的函数附加到窗口对象:
window.showAlert = function(msg, title, ele) {
var that = ele;
alertify.prompt(msg, title,
function(evt, value){
ele.closest('div').remove();
},
function(){
// do nothing
});
}
并将您的按钮更改为:
<button onclick="return showAlert('are you sure you want to delete', '{{obj.title}}')".....
<script src="//cdn.jsdelivr.net/npm/alertifyjs@1.13.1/build/alertify.min.js"></script>
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/alertifyjs@1.13.1/build/css/alertify.rtl.min.css"/>
<script type="text/javascript">
window.showAlert = function(msg, title, ele) {
var that = ele;
alertify.prompt(msg, title,
function(evt, value){
ele.closest('div').remove();
},
function(){
// do nothing
});
}
</script>
<div>
<p> This is the first post</p>
<button onclick="return showAlert('are you sure you want to delete', 'post n. 1', this)">Click Me</button>
</div>
<div>
<p> This is the second post</p>
<button onclick="return showAlert('are you sure you want to delete', 'post n. 2', this)">Click Me</button>
</div>
<div>
<p> This is the third post</p>
<button onclick="return showAlert('are you sure you want to delete', 'post n. 3', this)">Click Me</button>
</div>