Wierd jquery bug, when trying to append() after using wrap() [closed]

て烟熏妆下的殇ゞ 提交于 2019-12-20 04:32:23

问题


Using jquery:

1) Create a container

2) Wrap an element with it

3) Select container by passing an object to jquery selector and try to add an element to the container

Why doesn't element append?

Look at this example here: http://jsfiddle.net/Tngh4/

var container = document.createElement("div");
//Wrapping some random element with the container
$("#randomElem").wrap(container);

var elem = document.createElement("div");
//Selecting element this way and using append results in a strange bug
$(container).append(elem);

回答1:


.wrap wraps the element with a copy of the passed in element, not the passed in element itself.

A copy of this structure will be wrapped around each of the elements in the set of matched elements. This method returns the original set of elements for chaining purposes.

http://api.jquery.com/wrap

Here's your fiddle to use an alternative to .wrap http://jsfiddle.net/Tngh4/1/



来源:https://stackoverflow.com/questions/16200160/wierd-jquery-bug-when-trying-to-append-after-using-wrap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!