Difference between jQuery .hide() and .css(“display”, “none”)

后端 未结 7 1140
既然无缘
既然无缘 2020-12-12 14:33

Is there any difference between

jQuery(\'#id\').show() and jQuery(\'#id\').css(\"display\",\"block\")

and

jQuery(\'#id\').         


        
7条回答
  •  情书的邮戳
    2020-12-12 15:27

    jQuery('#id').css("display","block")

    The display property can have many possible values, among which are block, inline, inline-block, and many more.

    The .show() method doesn't set it necessarily to block, but rather resets it to what you defined it (if at all).

    In the jQuery source code, you can see how they're setting the display property to "" (an empty string) to check what it was before any jQuery manipulation: little link.

    On the other hand, hiding is done via display: none;, so you can consider .hide() and .css("display", "none") equivalent to some point.

    It's recommended to use .show() and .hide() anyway to avoid any gotcha's (plus, they're shorter).

提交回复
热议问题