如何用jQuery中的另一个包装现有的元素? [英] How to wrap an existing element with another one in jQuery?

查看:77
本文介绍了如何用jQuery中的另一个包装现有的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在现有的< div> 周围创建< div>



我的代码如下:

 < div id =test> ; 
< h2>问候语< / h2>
< p>测试< / p>
< div class =inner> Hello< / div>
< p>测试< / p>
< div class =inner> Goodbye< / div>
< / div>
< div id =test2>
< h2>问候语< / h2>
< p>测试< / p>
< div class =inner> Hello< / div>
< p>测试< / p>
< div class =inner> Goodbye< / div>
< / div>

< script> $($#$ b $(#test)。)('< div class =test3>'); ('< / div>');
$(#test2)。
< / script>

但它似乎无法插入< div> 没有结束标签。那么如何实现这一点呢?

使用 .wrapAll() doc 方法

  $('#test,#test2')。wrapAll('< div class =test3>'); 


How can I create a <div> surrounding an existing <div>.

My code is as follows:

<div id="test">
  <h2>Greetings</h2>
  <p>Test</p>
  <div class="inner">Hello</div>
  <p>Test</p>
  <div class="inner">Goodbye</div>
</div>
<div id="test2">
  <h2>Greetings</h2>
  <p>Test</p>
  <div class="inner">Hello</div>
  <p>Test</p>
  <div class="inner">Goodbye</div>
</div>

<script>
  $("#test").before('<div class="test3">');
  $("#test2").after('</div>');
</script>

But it seems that it cannot insert a <div> without an end tag. So how to achieve that?

解决方案

Use the .wrapAll() doc method

$('#test, #test2').wrapAll('<div class="test3">');

这篇关于如何用jQuery中的另一个包装现有的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆