简体   繁体   English

如何使用jQuery包装特定的多个div

[英]How to wrap particular multiple divs with jQuery

Just trying to work with jQuery wrapInner and I could not wrap particular multiple divs with a outer div. 只是尝试使用jQuery wrapInner,我无法用外部div包装特定的多个div。

 $('#outer').not('#inner3') .wrapInner('<div id="wrapper" style="background:green;"></div>'); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id='outer'> <div id='inner1'>1</div> <div id='inner2'>2</div> <div id='inner3'>3</div> </div> 

How to wrap inner1 and inner2 within another div. 如何将inner1和inner2包装在另一个div中。 I tried like above but this wraps whole 3 inner divs. 我尝试过如上所述,但这包含了整个3个内部div。

Output required : 需要输出

 <div id='outer'> <div id="wrapper" style="background:green;"> <div id='inner1'>1</div> <div id='inner2'>2</div> </div> <div id='inner3'>3</div> </div> 

Try $('#outer div').not('#inner3') 尝试$('#outer div').not('#inner3')

$(document).ready(function () {
    $('#outer div').not('#inner3')
        .wrapInner('<div id="hey" style="background:green;"></div>');
});

fiddle Demo 小提琴演示


$('#outer div').not('#inner3') select all div inside element with id outer but not with element with id inner3 $('#outer div').not('#inner3')选择具有ID的所有的div元素内outer但是用id元素inner3
if you want two put first two children you can use :lt() 如果你想要两个前两个孩子你可以使用:lt()

fiddle Demo 小提琴演示

$(document).ready(function () {
    $('#outer div:lt(2)')
        .wrapInner('<div id="hey" style="background:green;"></div>');
});


Update after OP's comment OP评论后更新

Updated fiddle Demo 更新了小提琴演示

 $(document).ready(function () { $('#outer div').not('#inner3') .wrapAll('<div id="hey" style="background:green;"></div>'); }); 

Use the .wrapAll() function: 使用.wrapAll()函数:

$('document').ready(function() {
  $('#outer div').not('#inner3')
    .wrapAll('<div id="hey" style="background:green;"></div>');
});

Check this link for more info. 请查看链接以获取更多信息。

From the documentation: 从文档:

Description: Wrap an HTML structure around all elements in the set of matched elements. 描述:围​​绕匹配元素集中的所有元素包装HTML结构。

.wrapAll( wrappingElement ) .wrapAll(wrappingElement)

wrappingElement wrappingElement

Type: Selector or htmlString or Element or jQuery 键入:Selector或htmlString或Element或jQuery

A selector, element, HTML string, or jQuery object specifying the structure to wrap around the matched elements. 一个选择器,元素,HTML字符串或jQuery对象,指定要包围匹配元素的结构。

.wrapAll( function ) .wrapAll(功能)

function 功能

Type: Function( Integer index ) => String or jQuery 类型:Function(整数索引)=> String或jQuery

A callback function returning the HTML content or jQuery object to wrap around the matched elements. 一个回调函数,返回HTML内容或jQuery对象以包装匹配的元素。 Receives the index position of the element in the set as an argument. 接收集合中元素的索引位置作为参数。 Within the function, this refers to the current element in the se 在函数内,这指的是se中的当前元素

To complete Tushar Gupta's answer : 要完成Tushar Gupta的答案

// wrapInner will add a div *inside each selected node*
$('#outer1 div').not('#inner13')
        .wrapInner('<div id="hey1" style="background:green;"></div>');

// wrap will add one wrapper around *each selected node*
$('#outer2 div').not('#inner23')
        .wrap('<div id="hey2" style="background:green;"></div>');

// to create one single wrapper, and put the divs you want inside,
// you can do as follows :
$('<div id="hey3" style="background:green;"></div>')
    .append( $('#outer3 div').not('#inner33') )
    .prependTo('#outer3');

fiddle 小提琴

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

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