只是尝试使用 jQuerywrappInner,我无法用外部 div 包装特定的多个 div。
$('#outer').not('#inner3')
.wrapInner('<div id="wrapper" style="background:green;"></div>');Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
如何将inner1和inner2包装在另一个div中。我尝试了上面的方法,但这包含了整个 3 个内部 div。
所需输出:
<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>Run Code Online (Sandbox Code Playgroud)
小智 2
Use the .wrapAll() function:
$('document').ready(function() {
$('#outer div').not('#inner3')
.wrapAll('<div id="hey" style="background:green;"></div>');
});
Run Code Online (Sandbox Code Playgroud)
Check this link for more info.
From the documentation:
Description: Wrap an HTML structure around all elements in the set of matched elements.
.wrapAll( wrappingElement )
wrappingElement
Type: Selector or htmlString or Element or jQuery
A selector, element, HTML string, or jQuery object specifying the structure to wrap around the matched elements.
.wrapAll( function )
function
Type: Function( Integer index ) => String or jQuery
A callback function returning the HTML content or jQuery object to wrap around the matched elements. 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