简体   繁体   English

jQuery wrap所有两个具有变量的元素

[英]jQuery wrapAll two element with variable

I have two variables currentSectionElement and sectionList . 我有两个变量currentSectionElementsectionList

They looks like: 它们看起来像:

currentSectionElement = $("<div>current</div>");
sectionList = $("<div>sectionList</div>");

I want to display it like this: 我想这样显示:

<div>
    <div>current</div>
    <div>sectionList</div>
<div>

So, I've tried following way: 因此,我尝试了以下方法:

currentSectionElement.after(sectionList); 
$(currentSectionElement, sectionList).wrapAll('<div></div>');

However it becomes: 但是它变成:

<div>
    <div>current</div>
<div>
<div>sectionList</div>

You can use .add() to combine the currentSectionElement and sectionList into a jQuery object then use . 您可以使用.add()currentSectionElementsectionList合并为一个jQuery对象,然后使用。 wrapAll()

 var currentSectionElement = $("<div>current</div>"); var sectionList = $("<div>sectionList</div>"); var newObj = currentSectionElement.add(sectionList); newObj.wrapAll('<div></div>').parent().appendTo(document.body); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 


You can create another div and use append() 您可以创建另一个div并使用append()

 var currentSectionElement = $("<div>current</div>"); var sectionList = $("<div>sectionList</div>"); var newObj = currentSectionElement.add(sectionList); $('<div>').append(newObj).appendTo(document.body); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

 currentSectionElement = $("<div>current</div>"); sectionList = $("<div>sectionList</div>"); $('body').append('<div class=red></div>'); $('body').find('div').append(currentSectionElement).append(sectionList) 
 .red{color:red} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Why not append an empty div then in that empty div append both divs 为什么不附加一个空的div,然后在该空的div中附加两个div

 currentSectionElement = $("<div>current</div>"); sectionList = $("<div>sectionList</div>"); $('body').append(currentSectionElement).append(sectionList) $('body').find('div').wrapAll('<div class=red></div>'); 
 .red{color:red} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

If you want wrap all append both in append then use wrapApp() 如果您想将所有附加都包装在append中,请使用wrapApp()

$(currentSectionElement, sectionList) doesn't create a jQuery set containing the intersection of currentSectionElement and sectionList . $(currentSectionElement, sectionList)不会创建包含currentSectionElementsectionList的交集的jQuery集。 To do that, you use currentSectionElement.add(sectionList) . 为此,请使用currentSectionElement.add(sectionList)

If you do that, your wrapAll works: 如果这样做,则wrapAll可以工作:

 currentSectionElement = $("<div>current</div>"); sectionList = $("<div>sectionList</div>"); currentSectionElement.add(sectionList) .wrapAll("<div></div>") .parent() .appendTo(document.body); 
 div { border: 1px solid black; padding: 4px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

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

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