【问题标题】:how to hide django message after it is displayed for few seconds如何在显示几秒钟后隐藏 django 消息
【发布时间】:2019-01-11 21:43:56
【问题描述】:

例如,在重定向之前,我使用了

messages.add_message(request, messages.INFO, 'You have successfully updated your listing.')

那么重定向后消息会自动显示,但是消息永远不会消失,请问几秒后怎么隐藏?

谢谢!!!

【问题讨论】:

    标签: javascript django


    【解决方案1】:

    您需要编写javascript代码,使用窗口setTimeout()方法在一段时间后隐藏消息。

    // suppose the `id` attribute of element is `message_container`.
    var message_ele = document.getElementById("message_container");
    
    setTimeout(function(){ 
       message_ele.style.display = "none"; 
    }, 3000);
    // Timeout is 3 sec, you can change it
    

    将此代码放在base.html 底部的<script> 标记内,这样每当页面在3 之后重新加载时 秒它会让你的消息消失。

    【讨论】:

      【解决方案2】:

      脚本:

      <script>
          setTimeout(function(){
              if ($('#msg').length > 0) {
                  $('#msg').remove();
              }
          }, 2000)    // 2000 millisecond
      </script>
      

      模板:

      <div id="msg">
          {{message}}
      </div>
      

      【讨论】:

        【解决方案3】:

        要仅隐藏带有特定标签的消息(例如仅 INFO 消息),您可以在 HTML 文档中使用以下代码。

        <script>
            // Get all info messages
            var info_messages = document.getElementsByClassName('alert-info');
        
            setTimeout(function(){
                for (var i = 0; i < info_messages.length; i ++) {
                    // Set display attribute as !important, neccessary when using bootstrap
                    info_messages[i].setAttribute('style', 'display:none !important');
                }
            }, 3000);
        </script>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-06-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-28
          相关资源
          最近更新 更多