JQuery中wrap,wrapAll,wrapInner的区别

wrap,wrapAll,wrapInner的区别

这三种包裹方法对于在文档中插入额外的结构化标记往往很有用,现在说明一下三者的区别:wrap是对每一个匹配的元素分别用他元素结构化标记包裹起来,wrapAll是将所有匹配的元素用一个其他元素的结构化标记包裹起来,wrapInner是对每一个匹配元素的内容用其他的结构化标记包裹起来,详细说明如下:
以下是需要包裹的按个段落:

<p>This is the first paragraph.</p>
<p>This is the second paragraph.</p>
<p>This is the third paragraph.</p>

现在用一个div进行包裹,其css样式如下:

div{
  background:green;
  float:left;
  margin:10px;
  width:180px;
  height:200px;
}

采用wrap进行包裹:

$("p").wrap("<div></div>");

效果:

<div><p>This is the first paragraph.</p></div>
<div><p>This is the second paragraph.</p></div>
<div><p>This is the third paragraph.</p></div>

这里写图片描述

采用wrapAll进行包裹:

$("p").wrapAll("<div></div>");

效果:

<div>
<p>This is the first paragraph.</p>
<p>This is the second paragraph.</p>
<p>This is the third paragraph.</p>
</div>

这里写图片描述

采用wrapInner进行包裹:

$("p").wrapInner("<div></div>");

效果:

<p><div>This is the first paragraph.</div></p>
<p><div>This is the second paragraph.</div></p>
<p><div>This is the third paragraph.</div></p>

这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值