Jquery wrapInner - 排除第一个元素

4 jquery wrapper

我试图使用图例标记上的click事件打开/折叠我的网站的部分字段集.但是我需要使用wrapInner在fieldset中添加一个div来隐藏内容......但是这也隐藏了传说(我肯定不想这样做):-).我如何使用wrapInner但指定不隐藏图例(或者是字段集中包含的第一个元素 - 因为它始终是图例).

$("#mainarea fieldset").wrapInner("<div class='fieldsetWrapper'></div>");

$("#mainarea fieldset:not(:first)").addClass("fsClosed"); // Close all fieldsets within the main area (but not the first one)

$("#mainarea fieldset legend").mousedown(function(){  // When clicking the legend of a fieldset ...
    $("#mainarea fieldset:not(.fsClosed)").addClass("fsClosed");  // If it's already open, close it
    $(this).parent().removeClass("fsClosed");  // If it's closed, remove the closed class from the containing fieldset
    return false;
}); 
Run Code Online (Sandbox Code Playgroud)

干杯马克

Sim*_*mon 7

为了回应你在Pim的例子中的评论,你需要遍历字段集

$('#mainarea fieldset').each(function(){
   $(this).children().not('legend').wrapAll("<div class='fieldsetWrapper'></div>");
});
Run Code Online (Sandbox Code Playgroud)

你可能可能会重构这样的事情;

$('#mainarea fieldset').each(function(){
   $(':not(legend)', this).wrapAll("<div class='fieldsetWrapper'></div>");
});
Run Code Online (Sandbox Code Playgroud)