开发者

hide() in jquery

In a jquery code, I am dynamically showing and hiding some content.

            if (this.hide()) {

                this.show();
                                }
            else {
                alert("Hello");
                this.hide();

            }

But else part is ne开发者_如何学Cver getting executed.why?


I'd do something like this :

if($(this).is(':hidden')) { 
    $(this).show();
}
else {
  $(this).hide();
}

Or depending on what you want to do, you can try toggle()


What you're looking for is

this.toggle();

The reason you never reach your 'else' clause is because hide() returns the elements found in the selector (for method chaining reasons). Since it always returns a valid object, it will always resolve to "truthy", i.e. not null, "", 0, false, etc.


this.hide() is running the hide function, not checking if it's hidden, thus it's setting it to hide and then running this.show()

as already said toggle() is what you want as it does the check for you without having to code for that.


In the if you are hidding the element. You should do this.is(':hidden')

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜