javascript - 查找并清除 toast (Toastr)

标签 javascript jquery angularjs toastr angular-toastr

我有一个页面,其中可能有多个 toasts 使用插件动态添加 https://github.com/CodeSeven/toastr .

我在每个 toast 上都有一个 link(Ok) 单击该链接我只需要关闭特定的 toast 而不是所有 toast 是可见的。

toastr.warning("You are warned. <br/> <a id='close-toastr'> Ok </a>", {
    tapToDismiss: false
    , timeOut: 0
    , extendedTimeOut: 0
    , allowHtml: true
    , preventDuplicates: true
    , preventOpenDuplicates: true
    , newestOnTop: true
});

$('body').on('click', 'a#close-toastr', function () 
    toastr.clear();
});

在上面的代码中,我使用了 toastr.clear() 方法来清除所有 toasts。

谁能帮我识别Ok linktoast 单击并只清除那个 toast?

更新 #1:

我尝试了@imjosh 给出的答案,但是 $(this).closest('.toast') 找到了正确的 toast 但是 toastr.clear($(this).closest ('.toast')); 不关闭任何 toast。

如果我将 toast object 存储在一个变量中,并将其作为参数传递给 toastr.clear(),它就可以工作。但是,我不知道如何以这种方式处理多个 toast 。

var toast = toastr.warning("You are warned. <br/> <a id='close-toastr'> Ok </a>", {
    tapToDismiss: false
    , timeOut: 0
    , extendedTimeOut: 0
    , allowHtml: true
    , preventDuplicates: true
    , preventOpenDuplicates: true
    , newestOnTop: true
});

$('body').on('click', 'a#close-toastr', function () 
    toastr.clear(toast);
});

更新#2:

抱歉,我正在使用 https://github.com/Foxandxss/angular-toastr插件不是我上面提到的那个。

谢谢。

最佳答案

toastr.options = {
    tapToDismiss: false
    , timeOut: 0
    , extendedTimeOut: 0
    , allowHtml: true
    , preventDuplicates: true
    , preventOpenDuplicates: true
    , newestOnTop: true
    , closeButton: true
    , closeHtml: '<button class="btn" style="background-color: grey; padding: 5px;">OK</button>'
}

toastr.warning("You are warned");
toastr.warning("You are warned 2");

https://jsfiddle.net/3ojp762a/3/

或者,如果出于某种原因需要,按照您尝试的方式进行:

toastr.warning("You are warned. <br/> <a class='close-toastr'> Ok </a>", "", {
    tapToDismiss: false
    , timeOut: 0
    , extendedTimeOut: 0
    , allowHtml: true
    , preventDuplicates: true
    , preventOpenDuplicates: true
    , newestOnTop: true
});

toastr.warning("You are warned2. <br/> <a class='close-toastr'> Ok </a>", "", {
    tapToDismiss: false
    , timeOut: 0
    , extendedTimeOut: 0
    , allowHtml: true
    , preventDuplicates: true
    , preventOpenDuplicates: true
    , newestOnTop: true
});


$('.close-toastr').on('click', function () {
    toastr.clear($(this).closest('.toast'));
});

https://jsfiddle.net/esgrwznu/

关于javascript - 查找并清除 toast (Toastr),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41040911/

相关文章:

javascript - 我需要一个正则表达式来匹配字符串

javascript - 使用 Javascript 以编程方式将 Google Chrome 置于全屏模式?

javascript - 如何在 TypeScript 中正确引用模块?

php - 为什么这个 POST 变量通过 AJAX Null 发送? (jquery/php)

javascript - 复选框标签无法与 Angular 一起正常工作

javascript - 在 AngularJS 中访问对象的参数

javascript - 基于范围 slider 值从底部淡入的背景

angularjs - 有什么方法可以使用指令动态添加 ng-model-options 吗?

javascript - 如何在 $scope 中分离 get http 请求中的 json 对象?

javascript - Chartjs - 将附加数据插入图表工具提示