简体   繁体   中英

Jquery addClass Not working

I don't really know what I'm doing wrong. I have tried severally and it's still not working. Here is my code.

I want <p>content</P> content to disappear after 3seconds

$loginmessage = "<br /><p class='alert alert-danger'>Login Fail</p>";
<div id="alertarea" class="text-center"><?php if(isset($loginmessage)) echo $loginmessage ?></div>

$("document").ready(function(){
    function hideWithTime(){
        if($("div#alertarea p").html != ""){
            $(this).addClass('hideit');
        }
    }
    // setTimeout(hideWithTime(), 2000);
    hideWithTime();
});

Its html() - a function not html - a property

Cache your element, this doesn't refer to $("div#alertarea > p")

$("document").ready(function(){
       var element = $("div#alertarea > p");
        function hideWithTime(){
            if(element.html() != ""){
               element.addClass('hideit');//ps bootstrap has a hide class
            }
        }

         setTimeout(hideWithTime, 2000); 
    });`

If the child of the div is just a p tag, your code finds always no html. better do:

function hideWithTime(){
    if($("div#alertarea").html() != ""){
            $("div#alertarea>p").addClass('hideit');
        }
    }

谢谢您的支持。

setTimeout (function(){ if($("div#alertarea").html() != ""){ $("div#alertarea>p").addClass('hideit'); } }, 5000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM