javascript - Toastr 不会遵循这些选项

标签 javascript toastr

是的,我知道,5 月 22 日(一周前)有相同问题的答案,但我尝试了相同的解决方案,它是在实际“调用”toastr 脚本之前指定的,我已经甚至尝试了 : 和 = 但我无法解决问题。

第一个代码是整个页面,包括“检查是否设置了cookie,如果没有,显示toastr并设置cookie”脚本。

除了“选项”之外,这个可以工作。 它在屏幕上显示 2 toastr

<!DOCTYPE html><html><head><meta charset=utf-8>

<link href="https://my.site.se/js/toastr.css" rel="stylesheet" type="text/css" />

<script src= "https://my.site.se/js/jquery-3.2.1.js"></script>
<script src="https://my.site.se/js/toastr.js"></script>
<!-- createCookie funktion -->
<script>

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}


</script>
<script>
<!-- see if the i agree cookie is set, else send them to the info page -->

function readCookie(name) {
    var nameEQ = name + "=";

    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);

    }



toastr.options = {
  "closeButton": true,
  "debug": false,
  "newestOnTop": false,
  "progressBar": true,
  "positionClass": "toast-top-left",
  "preventDuplicates": false,
  "showDuration": "300",
  "hideDuration": "1000",
  "timeOut": "5000",
  "extendedTimeOut": "1000",
  "showEasing": "swing",
  "hideEasing": "linear",
  "showMethod": "fadeIn",
  "hideMethod": "fadeOut"

}



$(function () {

toastr.options.closeButton: true;
toastr.options.progressBar=true;
toastr["error"]("We use Cookies!","Cookies!")

});



$(function () {toastr["info"]("We have now created a cookie that prevents this information from being displayed again, unless you remove the cookie of course\n Read the terms page for more info\n To delete ALL your cookies, press the delete cookies button!")});


}

</script>
</Head>
<body>

<script>
readCookie('acceptterms');
</script>
<script>
createCookie('acceptterms','yesIdo',3650);
</script>

代码在头部,如果我把它放在正文中,它根本不起作用,我什至尝试过:

$(function () {toastr.option.progressBar= true;
toastr["error"]("We use Cookies!","Cookies!")
});

我也尝试过设置

toastr.options = {   "closeButton": true,   "debug": false,   "newestOnTop": false,   "progressBar": true,   "positionClass": "toast-top-left",   "preventDuplicates": false,   "showDuration": "300",   "hideDuration": "1000",   "timeOut": "5000",   "extendedTimeOut": "1000",   "showEasing": "swing",   "hideEasing": "linear",   "showMethod": "fadeIn",   "hideMethod": "fadeOut" }

在 body 里.. 还有一些我现在已经想不起来了.. 我只是不明白为什么设置选项这么难......

如果可以在脚本本身中更改选项会更容易,但有些我在那里找不到..

最佳答案

您可以在调用 toast 时将选项设置为第三个参数。

在控制台中尝试一下:

toastr["error"]("We use Cookies!","Cookies!", {"progressBar": true,"closeButton": true, "positionClass": "toast-top-full-width"});

此外,也许您的选项没有被读取,因为 readCookie(name) 函数没有用分号结束

关于javascript - Toastr 不会遵循这些选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44257138/

相关文章:

angularjs - ngx-toastr GlobalConfig 超时设置

Javascript:想要找到句子中以@开头的所有单词

javascript - 在 Chrome 中使用 setTimeout 和 showModalDialog 会阻止选项卡

javascript - 剥离表格内容

javascript - 如何过滤具有相同名称但不同值的项目的数组?

javascript - Angular slider 不显示

jquery - 分别为每种 toast 类型设置 toastr.js 选项

javascript - 使用 toastr.js 时 toastr 未定义

javascript - Controller 方法完成后在 toastr 中显示消息