문제

<p>hello</p>
$("p").wrap("<div class='inner'></div>").wrap("<div class='outer'></div>");

The result is <div class="inner"><div class="outer"><p>hello</p></div></div>,

Why is it so? I expected inner inside outer, not otherwise.

Here is fiddle to play with : http://jsfiddle.net/qnYDY/

도움이 되었습니까?

해결책 2

Because the wrap() method:

...returns the original set of elements for chaining purposes.

Source: http://api.jquery.com/wrap/

다른 팁

I think the first wrap call returns "p" itself , so when you wrap it again, the "outer" div will wrap the "p" tag and not any of its parents.

As stated by Jamie Dixon wrap() returns the original set of arguments.

You could use parent() to append to the newly added element:

$("p").wrap("<div class='inner'></div>").parent().wrap("<div class='outer'></div>");
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top