What does $($(this)) mean?

后端 未结 6 1420
借酒劲吻你
借酒劲吻你 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:06

    Yes, $($(this)) is the same as $(this), the jQuery() or $() function is wonderfully idempotent. There is no reason for that particular construction (double wrapping of this), however, something I use as a shortcut for grabbing the first element only from a group, which involves similar double wrapping, is

    $($('selector')[0])

    Which amounts to, grab every element that matches selector, (which returns a jQuery object), then use [0] to grab the first one on the list (which returns a DOM object), then wrap it in $() again to turn it back into a jQuery object, which this time only contains a single element instead of a collection. It is roughly equivalent to

    document.querySelectorAll('selector')[0];, which is pretty much document.querySelector('selector');

提交回复
热议问题