我正在尝试打开/折叠我的网站的部分,这些部分是带有图例标记上的单击事件的字段集。然而,我需要使用一个wrapInner在字段集中添加一个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;
});
干杯 标记
最佳答案
为了回应您在 Pim 示例中的评论,您需要循环遍历字段集
$('#mainarea fieldset').each(function(){
$(this).children().not('legend').wrapAll("<div class='fieldsetWrapper'></div>");
});
您可能可以将其重构为类似的内容;
$('#mainarea fieldset').each(function(){
$(':not(legend)', this).wrapAll("<div class='fieldsetWrapper'></div>");
});
关于Jquery wrapInner - 排除第一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/459447/