2013-04-24 16 views
1

使用jQuery:奇怪的jquery錯誤,使用包裹物後當試圖追加()()

1)創建一個容器

2)與它

3)通過選擇容器包裹的元件傳遞一個對象到jquery選擇器並嘗試添加一個元素到容器中

爲什麼不添加元素?

在這裏看看這個例子: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與元素傳遞的,而不是元素本身所傳遞的副本包裝的元素。

此結構的副本將包裹在匹配元素集合中的每個元素周圍。該方法返回用於鏈接目的的原始元素集合。

http://api.jquery.com/wrap

這是你的小提琴使用替代.wraphttp://jsfiddle.net/Tngh4/1/

+0

THX。現在看起來很明顯。我無法相信我錯過了它。 – CoolCodeBro 2013-04-24 19:16:10