jQuery:附加到父级 [英] jQuery: appendTo parent
问题描述
我似乎无法让 appendTo 工作.我做错了什么?
$('div:nth-child(2n) img').appendTo(parent);
当前标记:
<img src="123.jpg"/><p>嘿</p><div class="容器"><img src="123.jpg"/><p>嘿</p>
我想要这个输出:
<p>嘿</p><img src="123.jpg"/><div class="容器"><p>嘿</p><img src="123.jpg"/>
请帮帮我……我每一分钟都在撕裂我的头发……:-S
以下内容就足够了:
$("div>img").each(function(){$(this).appendTo($(this).parent());});
看到它在这里工作:http://jsfiddle.net/EtxqL/
您无法从 appendTo() 的 'selector' 参数推断每个项目的父项.做你想做的唯一方法是遍历项目,将每个项目附加到其父项.查看以下链接中的 API.
I can't seem to get the appendTo to work. What do I do wrong?
$('div:nth-child(2n) img').appendTo(parent);
Current markup:
<div class="container">
<img src="123.jpg" />
<p>Hey</p>
</div>
<div class="container">
<img src="123.jpg" />
<p>Hey</p>
</div>
I want this output:
<div class="container">
<p>Hey</p>
<img src="123.jpg" />
</div>
<div class="container">
<p>Hey</p>
<img src="123.jpg" />
</div>
Please help me guys... I'm tearing my hair of every minute.. :-S
The following should suffice:
$("div>img").each(function(){
$(this).appendTo($(this).parent());
});
See it working here: http://jsfiddle.net/EtxqL/
You can't infer each item's parent from the 'selector' parameter to appendTo(). The only way to do what you want is to loop through the items, appending each one to its parent. Check out the APIs in the following link.
这篇关于jQuery:附加到父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!