What does $($(this)) mean?

后端 未结 6 1497
借酒劲吻你
借酒劲吻你 2021-02-04 23:53

I saw some code around the web that uses the following statement

if ($($(this)).hasClass(\"footer_default\")) {
      $(\'#abc\')
        .appendTo($(this))
             


        
6条回答
  •  耶瑟儿~
    2021-02-05 00:08

    As explained before me, $($(this)) and $(this) are absolutely identical. jQuery returns the same jQuery object if you try to wrap it more than once.

    Additionally, for performance considerations it is a good practice to reuse jQuery objects - it is quite expensive to create jQuery objects, especially the ones with complex selectors. Example:

    var $this = $(this);
    if ($this.hasClass("footer_default")) {
        $('#abc')
            .appendTo($this)
            .toolbar({position: "fixed"});
    }
    

    Just google for 'jQuery best practices' - it will take a 30 min for you to learn these basics and you will use jQuery way more effectively.

提交回复
热议问题