题
我试图打开与对传说标签的单击事件的fieldsets我的网站/折叠部分。但是我将需要使用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;
});
干杯 标记
解决方案
在响应皮姆的例子您的意见,您可以通过字段集需要循环
$('#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>");
});
其他提示
$('#mainarea fieldset').children(':gt(0)').wrapAll("<div class='fieldsetWrapper'></div>");
这应该做的伎俩。结果 在wrapAll功能简介: http://docs.jquery.com/Manipulation/wrapAll#html >
修改的结果, 甚至可能更好:
$('#mainarea fieldset').children().not('legend').wrapAll("<div class='fieldsetWrapper'></div>");
我最终使用以下解决方案:
//Wrap everyting in the fieldset tags
$('#mainarea fieldset').wrapInner("<div class='fieldsetWrapper'></div>");
//for each legend tag move it out of the newly created wrapping div
$('legend').each(function(){
$(this).insertBefore($(this).parent());
});
首先,它包裹字段集的标签(包括图例)内一切那么它解开'的图例标签。
$(document).ready(function(){
$("fieldset legend").click(function(){
$(this).parent().children().not('legend').toggle("slow");
});
});
不隶属于 StackOverflow