doujuanju3076 2012-05-09 12:50 采纳率: 0%
浏览 28

CSS通知消失

Im creating a small web app and I have used AJAX in a few functions such a creating and deleting certain objects.

To notify the user I have used PHP to echo a HTML notification on to the screen depending on weather the object was successfully created or not.

if ($query) {
 //response to ajax call if successful
  echo '<div class="alert alert-success"><a class="close" data-dismiss="alert">×</a><h4 class="alert-heading">Success!</h4>Object Added!</div>';

}

The problem is, over time the notifications build up on the screen as there is no refresh to remove them.

I have a jQuery function that can remove the alerts every 5 seconds shown below

function clearAlerts(){
        setTimeout(function() {
            $('.alert').fadeOut('fast');
        }, 5000);
    }

But I dont know how to call this function every time a notification is added to the page, is there anyway to use jQuery to perhaps detect when a notification has been echoed on to the page, or to run this jQuery function each time they are added.

Thanks

  • 写回答

2条回答 默认 最新

  • drfm55597 2012-05-09 12:55
    关注

    Put this tag directly inside each HTML snippet you are generating, you can style it in any way you like:

    <span class="close-notification">Close</span>
    

    Then use this piece of JS to remove it when clicked.

    $(document).on('click', '.close-notification', function(){
        $(this).parent().fadeOut(); // could use .remove(), .slideUp() etc
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统