English 中文(简体)
只有在空闲的情况下才能驱逐DIV
原标题:Remove DIV only if empty

我有一套PHP通知系统,通知的数量将用 j子输入DIV。 唯一的问题是,当有0份通知时,空洞的DIV仍然显示出来。 这是我目前正在使用的 j:

$(document).ready(function() {
    $.get( /codes/php/nf.php , function(a) {
        $( #nfbadge ).html(a);
        $( #nfbadge:empty ).remove();
    })
});
setInterval(function() {
    $.get( http://localhost/codes/php/nf.php , function(a) {
        $( #nfbadge ).html(a);
        $( #nfbadge:empty ).remove();
    })
}, 8000);

The only problem is that if at document load there is 0 notifications and a notification is added, the badge will not show up, so basically if the element is removed it won t come back unless the page is reloaded, but I made the notification system so that the page wouldn t have to be reloaded. How can I fix this?

最佳回答

If you have control over the PHP, you shouldn t be using jQuery to be removing DIVs, it s a waste of resources and load time, even if it s just a few lines of code.

在你的PHP模板中,应当列入#nfbadge。 div 有条件发言,例如:

if($notifications) {
     echo  <div id="nfbadge"> ;
     //notification stuff
     echo  </div> ;
}

Then with your jQuery code you could do something like the following:

 var $nfbadge = $( #nfbadge );
 if($nfbadge) {$nfbadge.html(a)}
问题回答

.remove() takes the element out of the DOM as well as the content. This is why it doesn t come back unless you reload. Use .fadeOut() or .hide() instead

或许,你们应该做这样的事情:

var elm = $( #nfbadge ),
      T = setInterval(getCodes, 8000);

function getCodes() {
    $.get( /codes/php/nf.php , function(a) {
        elm.html(a);
        if (elm.is( :empty ) && elm.is( :visible )) {
            elm.hide();
        }else{
            elm.show();
        }
   });
}

Will need some more work on your part, but should get you on the right track!





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签