0

我刚刚开始使用 grutter 通知插件并遇到奇怪的行为。我在页面加载时显示通知,出于某种原因,相同的消息出现了两次。如果有人可以检查我的代码并突出显示我忽略的任何错误,我将不胜感激。该插件的链接是:http ://boedesign.com/blog/2009/07/11/growl-for-jquery-gritt/ 。非常感谢。

来自 Firebug 的更新 HTML

<div id="gritter-notice-wrapper">

    <div id="gritter-item-2" class="gritter-item-wrapper" role="alert" style="">
        <div class="gritter-top"></div>
        <div class="gritter-item">
            <a class="gritter-close" tabindex="1" href="#" style="display: none;">

                Close Notification

            </a>
            <div class="gritter-without-image">
                <span class="gritter-title">

                    New Message

                </span>
                <p>

                    You have  

                    <span class="yourstyle">

                        1

                    </span>

                     new message(s). Please go to the message area in …

                </p>
            </div>
            <div style="clear:both"></div>
        </div>
        <div class="gritter-bottom"></div>
    </div>


    <div id="gritter-item-3" class="gritter-item-wrapper" role="alert" style="">
        <div class="gritter-top"></div>
        <div class="gritter-item">
            <a class="gritter-close" tabindex="1" href="#">

                Close Notification

            </a>
            <div class="gritter-without-image">
                <span class="gritter-title">

                    New Message

                </span>
                <p>

                    You have  

                    <span class="yourstyle">

                        1

                    </span>

                     new message(s). Please go to the message area in …

                </p>
            </div>
        </div>
        <div class="gritter-bottom"></div>
    </div>

</div>  


<div id="messages" style="float: left;height: 220px">
     <div>
       Messages
     </div>
     <div style="overflow: hidden;">
        <div id="message"style="font-size: 11px; padding: 10px 10px;" >

            <?php

                mysql_select_db($database_domain, $domain); 
                $query1 = "SELECT * FROM messages WHERE to_user = '$_SESSION[kt_name_usr]' AND `read` = 1"; 
                $result1 = mysql_query($query1) or die(mysql_error());
                $totalRows1 = mysql_num_rows($result1);
                if (mysql_num_rows($result1) ==0) {
                $error1 = true;
                $message3 = "You have no new messages.";
                } else {
                // There was a result
                // now check if there were any records.
                    if (mysql_num_rows($result1) >0) {    
                        $error1 = false;
                        $messages1 = "You have" . "  " . "<span class='yourstyle'>" . $totalRows1 . "</span>" . " " . "new message(s). Please go to the message area in your control panel."; 
                  }
                 } 

                 if($error1 == 0)  {

                    echo "You have" . "  " . "<span class='yourstyle'>" . $totalRows1 . "</span>" . " " . "new message(s). Please go to the message area in your control panel.";

                    echo "<SCRIPT type=text/javascript>\n";
                    echo "$(function() {\n";
                    echo "$(\"#testid\" ).show()\n";
                    echo "$.gritter.add({title: 'This is a notice', text: \"".$messages1."\"})\n";
                    echo "});\n";
                    echo "</script>\n";

                } else { 

                    echo 'There are no new system or site messages to display.';

                }

             ?>

            </div>

           </div>

          </div>

          <div id="testid" style="display:none;"></div>

解决方案:

<script type="text/javascript">
       $(function () {
            var newmsg = <?php echo json_encode($messages1) ?>;
            $.gritter.add({title: 'New Message', text: newmsg, time: 4000, sticky: false});
    });
</script>
4

2 回答 2

0

只需注释掉这一行,

echo "You have" . "  " . "<span class='yourstyle'>" . $totalRows1 . "</span>" . " " . "new message(s). Please go to the message area in your control panel.";
于 2014-09-19T11:17:50.870 回答
0

我通过 stopImmediatePropagation() 解决了同样的问题

尝试使用以下代码

    <script type="text/javascript">
       $(function (event) {
            event.stopImmediatePropagation();
            var newmsg = <?php echo json_encode($messages1) ?>;
            $.gritter.add({title: 'New Message', text: newmsg, time: 4000, sticky: false});
    });
</script>
于 2018-05-10T06:50:31.580 回答