简体   繁体   English

jQuery Dom 操作:使用 jQuery 在 div 周围添加包装 div

[英]jQuery Dom Manipulation : Adding wrapper div around a div using jQuery

when I'm designing a website I came across this issue .当我设计一个网站时,我遇到了这个问题。 I had to create a wrapper DIV around a DIV in my html code.我必须在我的 html 代码中围绕 DIV 创建一个包装 DIV。 So I'm asking this Question because this will be useful to others in future所以我问这个问题是因为这将来对其他人有用

I wan't to add a wrapper div to this div using jQuery我不想使用 jQuery 向这个 div 添加一个包装 div

<div class="innerdiv"></div>

This has probably been asked and answered, but here is my response:这可能已经被问到并得到了回答,但这是我的回应:

First, see jQuery documentation首先看jQuery文档

Second, here is the simple answer:其次,这是一个简单的答案:

$( ".innerdiv" ).wrap( "<div class='new'></div>" );

jQuery provides a method for you: jQuery 为您提供了一种方法:

$('.innerdiv').wrap('<div class="outerdiv"></div>');

http://api.jquery.com/wrap/ http://api.jquery.com/wrap/

you can wrap a div with another div using jQuery .wrap()你可以使用 jQuery .wrap()用另一个 div 包裹一个 div

<div class="innerdiv"></div>

in the jQuery code you have to select the innerdiv and then you have to create the wrapper div insdie the wrap() .在 jQuery 代码中,您必须选择 innerdiv,然后您必须在wrap() 中创建包装器 div。 and you need to import jQuery to run this.你需要导入 jQuery 来运行它。

 $( ".innerdiv" ).wrap( "<div class='new'></div>" );

checkout my codepen link here在此处查看我的 codepen 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM