jQuery:附加到父级 [英] jQuery: appendTo parent

查看:35
本文介绍了jQuery:附加到父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法让 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.

.appendTo() API

.每个 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.

.appendTo() API

.each API

这篇关于jQuery:附加到父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆