如何实现 Toastr JS?

Posted

技术标签:

【中文标题】如何实现 Toastr JS?【英文标题】:How do I implement Toastr JS? 【发布时间】:2013-05-09 03:07:01 【问题描述】:

我是 JS 新手,不知道如何在我的页面上进行这项工作。下面是我所拥有的。我该如何制作这个警报显示?

我正确添加了源,但不确定如何呈现警报。

<!doctype html>
    <html>
    <head>
    <title>Toast</title>
    <link href="toastr.css" rel="stylesheet"/>
    <script src="toastr.js"></script>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
    <script>
    $(document).ready(function() 
    //toastr.info('Are you the 6 fingered man?')


    Command: toastr[success]("   ", "Settings Saved!")

    toastr.options: 
    "debug": false,
    "positionClass": "toast-top-right",
    "onclick": null,
    "fadeIn": 300,
    "fadeOut": 1000,
    "timeOut": 5000,
    "extendedTimeOut": 1000
    
    );
    </script>
   </head>
    <body>
    </body>
    </html>

【问题讨论】:

我希望你已经开始使用documentation 我做了,但我不清楚如何调用警报?我是否使用点击事件?我也是 JS 新手。 toastr 前需要删除Command: 【参考方案1】:

Toastr 是一个非常不错的组件,您可以使用以下命令显示消息:

// for success - green box
toastr.success('Success messages');

// for errors - red box
toastr.error('errors messages');

// for warning - orange box
toastr.warning('warning messages');

// for info - blue box
toastr.info('info messages');

如果您想在 toastr 消息上提供标题,只需添加第二个参数:

// for info - blue box
toastr.success('The process has been saved.', 'Success');

您还可以使用以下方式更改默认行为:

toastr.options.timeOut = 3000; // 3s

在github of the project 上查看更多信息。

编辑

使用示例:

$(document).ready(function() 

    // show when page load
    toastr.info('Page Loaded!');

    $('#linkButton').click(function() 
       // show when the button is clicked
       toastr.success('Click Button');

    );

);

和一个html:

<a id='linkButton'>Show Message</a>

【讨论】:

谢谢。但我的问题是如何在我的 html 中使用它们来显示这些消息。谢谢! 嗯,你已经在你的页面上设置了它吗?只需在需要显示消息的地方调用那些方法 :).. 例如,在 $(document).ready(function() ...); 或任何其他事件处理程序上! 这就是我的意思。我怎么称呼他们?如果这很明显,我很抱歉,但我是 JS 新手。 你能用我在问题中的代码给我一个示例吗?我会很感激的。谢谢! 这样的? $('.show_hide').click(function () // 成功 - 绿框 toastr.success('Success messages'); toastr.options.timeOut = 3000; // 3s );【参考方案2】:

您不需要 jquery-migrate。总结以前的答案,这是一个有效的html:

<html>
  <body>
    <a id='linkButton'>ClickMe</a>
      
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
      
    <link href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/css/toastr.css" rel="stylesheet"/>
      
    <script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/js/toastr.js"></script>
      
    <script type="text/javascript">
      $(document).ready(function() 
        toastr.options.timeOut = 1500; // 1.5s
        toastr.info('Page Loaded!');
        $('#linkButton').click(function() 
           toastr.success('Click Button');
        );
      );
    </script>
  </body>
</html>

【讨论】:

【参考方案3】:

我进行了调查,我知道 jquery 脚本需要加载才能在您的情况下无法正常工作。 因为除非您首先加载 Jquery 1.9.1,否则代码中提到的 $ 符号无法理解。 如下加载

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/css/toastr.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/js/toastr.js"></script>

然后就可以正常使用了

【讨论】:

【参考方案4】:

这是一种简单的方法!

<link href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/css/toastr.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/js/toastr.js"></script>
<script>
function notificationme()
toastr.options = 
            "closeButton": false,
            "debug": false,
            "newestOnTop": false,
            "progressBar": true,
            "preventDuplicates": true,
            "onclick": null,
            "showDuration": "100",
            "hideDuration": "1000",
            "timeOut": "5000",
            "extendedTimeOut": "1000",
            "showEasing": "swing",
            "hideEasing": "linear",
            "showMethod": "show",
            "hideMethod": "hide"
        ;
toastr.info('MY MESSAGE!');

</script>

【讨论】:

【参考方案5】:

添加toastr.css和toastr.js的CDN文件

<link href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/css/toastr.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/js/toastr.js"></script>

function toasterOptions() 
    toastr.options = 
        "closeButton": false,
        "debug": false,
        "newestOnTop": false,
        "progressBar": true,
        "positionClass": "toast-top-center",
        "preventDuplicates": true,
        "onclick": null,
        "showDuration": "100",
        "hideDuration": "1000",
        "timeOut": "5000",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "show",
        "hideMethod": "hide"
    ;
;


toasterOptions();
toastr.error("Error Message from toastr");

【讨论】:

是否需要将选项放在函数中? 不,没有必要。 Toastr 将应用默认值。如果您需要对 Toastr 进行任何自定义,那么您可以。如果没有,直接调用Success, Error, Info,Warning Toastr's。

以上是关于如何实现 Toastr JS?的主要内容,如果未能解决你的问题,请参考以下文章