出于某种原因,我无法使用存储在数据活动属性中的数据激活手风琴面板?
我的HTML看起来像这样(在它转向手风琴并添加了所有类之后):
<div class="gcAccordion ui-accordion ui-widget ui-helper-reset" style="" data-active="1" role="tablist">
<h3 class="gcAccordionTitle ui-accordion-header ui-helper-reset ui-state-default ui-corner-all ui-accordion-icons" style="display: block;" role="tab" id="ui-accordion-1-header-0" aria-controls="ui-accordion-1-panel-0" aria-selected="false" tabindex="0"><span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-e"></span>This is a title</h3>
<div class="gcAccordionContent ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom" style="display: none;" id="ui-accordion-1-panel-0" aria-labelledby="ui-accordion-1-header-0" role="tabpanel" aria-expanded="false" aria-hidden="true">This is some content</div>
<h3 class="gcAccordionTitle ui-accordion-header ui-helper-reset ui-state-default ui-corner-all ui-accordion-icons" style="display: block;" role="tab" id="ui-accordion-1-header-1" aria-controls="ui-accordion-1-panel-1" aria-selected="false" tabindex="-1"><span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-e"></span>This is a 2nd title</h3>
<div class="gcAccordionContent ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom" style="display: none;" id="ui-accordion-1-panel-1" aria-labelledby="ui-accordion-1-header-1" role="tabpanel" aria-expanded="false" aria-hidden="true">This is some 2nd content</div>
<h3 class="gcAccordionTitle ui-accordion-header ui-helper-reset ui-state-default ui-corner-all ui-accordion-icons" style="display: block;" role="tab" id="ui-accordion-1-header-2" aria-controls="ui-accordion-1-panel-2" aria-selected="false" tabindex="-1"><span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-e"></span>This is a 3rd title</h3>
<div class="gcAccordionContent ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom" style="display: none;" id="ui-accordion-1-panel-2" aria-labelledby="ui-accordion-1-header-2" role="tabpanel" aria-expanded="false" aria-hidden="true">This is some 3rd content</div>
</div>
使用Javascript:
$('.gcAccordion').accordion({
collapsible: true,
heightStyle: "content",
autoHeight: false,
active: false,
create: function(event, ui){
$('.gcAccordion').each(function(){
console.log($(this).attr('data-active'));
$(this).accordion( "option", "active", $(this).attr('data-active'));
});
}
});
console.log调用正确地向日志输出1但由于某种原因它不会激活第二个面板。如果我在javascript中的第9行使用这一行,它可以工作:
$(this).accordion( "option", "active", 1);
但它不适用于数据活动属性1值。有什么想法正在发生什么?
编辑:删除链接,因为页面不再处于活动状态,并且所有代码都在问题中。
更新
感谢您的帮助。我需要parseInt($(this).attr('data-active'))
答案 0 :(得分:3)
.attr()
返回的值类型是一个字符串。我打赌你需要一个真/假(0/1)值,所以试试:
$(this).accordion("option", "active", +$(this).attr('data-active'));
+
一元运算符类似于parseInt
,将字符串转换为数字,但解析不太严格,而且速度更快(我认为)。
可能更好的是:
$(this).accordion("option", "active", $(this).attr('data-active') === "1" ? 1 : 0);
只有当data-active
属性存在且其值为“1”时,才会激活折叠窗格。存在/值的任何其他组合都不会打开窗格。
修改强>
忽略第二个例子,因为“active”选项接受基于0的整数,正如OP指出的那样。我认为它出于某种原因接受了真/假。