jQuery wrap() only works on the DOM, not on jQuery object?

后端 未结 2 1083
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-21 08:25

Given the following code in javascript with jQuery:

var inner = $(\'

\') var outer = inner.wrap(\'
\') console.log(inner
2条回答
  •  半阙折子戏
    2021-01-21 09:09

    .wrap() works also on jQuery objects that do not reference anything in the DOM, like $('

    ').

    But you have to properly ask the result. Due to chaining, you only get the original jQuery object, but you have to ask for the newly created elements around it with .parents() to get your expected result.

    $('

    ').wrap('
    ').parents()

    will give:


    notes from the OP

    It's was obvious I was not getting it before, but due to the answer and comments from @jfriend00 I became to grasp it. Also I was in doubt if a jQuery object is only a reference to something in the DOM or could be something entirly new.

提交回复
热议问题