2015-09-11 36 views
0

這裏是我的問題:jQuery選擇憑身份證不能正常工作

我有一組由一個jQuery腳本管理3個手風琴框,如下圖所示:

jQuery(document).ready(function($) { 
 
    jQuery('.accordion .accordion-section-content').hide(); 
 
    jQuery('.accordion .accordion-section-title:first').addClass('active').next().show(); 
 
    jQuery('.accordion .accordion-section-title').click(function(){ 
 
     if(jQuery(this).next().is(':hidden')) { 
 
      jQuery('.accordion .accordion-section-title').removeClass('active').next().slideUp(); 
 
      jQuery(this).toggleClass('active').next().slideDown(); 
 
     }else { 
 
\t  jQuery(this).removeClass('active').next().slideUp(); 
 
     } 
 
     return false; // Prevent the browser jump to the link anchor 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div class="accordion"> 
 
\t \t \t <div class="accordion-section"> 
 
\t \t \t \t <a class="accordion-section-title" style="text-align: center;" href="">Title1</a> 
 
\t \t \t \t <div id="accordion-1" class="accordion-section-content"> 
 
\t \t \t \t \t <p>Some text</p> 
 
\t \t \t \t </div><!--end .accordion-section-content--> 
 
\t \t \t </div><!--end .accordion-section--> 
 

 
\t \t \t <div class="accordion-section"> 
 
\t \t \t \t <a class="accordion-section-title" style="text-align: center;" href="">Title2</a> 
 
\t \t \t \t <div id="accordion-2" class="accordion-section-content"> 
 
\t \t \t \t \t <p>Some text.</p> 
 
\t \t \t \t </div><!--end .accordion-section-content--> 
 
\t \t \t </div><!--end .accordion-section--> 
 

 
\t \t \t <div class="accordion-section"> 
 
\t \t \t \t <a class="accordion-section-title" style="text-align: center;" href="">Title3</a> 
 
\t \t \t \t <div id="accordion-3" class="accordion-section-content"> 
 
\t \t \t \t \t <p>Some text.</p> 
 
\t \t \t \t </div><!--end .accordion-section-content--> 
 
\t \t \t </div><!--end .accordion-section--> 
 
\t \t </div><!--end .accordion-->

正如你可以看到第二個jQuery將第一個手風琴盒初始化爲在頁面加載時可見,這很好。但我希望能夠顯示第二個,即中間的一個。因此,我試圖通過

jQuery('#accordion-2') 
jQuery('.accordion #accordion-2') 
jQuery('.accordion').find('#accordion-2') 
jQuery('.accordion .accordion-section-title #accordion-2') 

和其他各種可能性,我能找到選擇由ID(我相信在這種情況下,合適嗎?)的元素來改變

jQuery('.accordion .accordion-section-title:first') 

,沒有什麼不...

我不明白爲什麼。

回答

0

jQuery('.accordion .accordion-section-title:first')選擇<a class="accordion-section-title" style="text-align: center;" href="">Title1</a>

jQuery('#accordion-2')選擇<div id="accordion-2" class="accordion-section-content">

你需要jQuery('#accordion-2').prev()

+0

媽,我累了...謝謝你這麼多,不能相信我沒看出來! :-) –

0

我相信

jQuery('.accordion .accordion-section-title:eq(1)') 

應該做的伎倆。 :eq(1)將過濾掉基於0的列表中的第二個DOM元素。

0

問題不在於你的jQuery選擇,它是用的.next();

試試這個(測試它,它的工作原理):

jQuery(document).ready(function($) { 
    jQuery('.accordion .accordion-section-content').hide(); 
    jQuery('#accordion-2').addClass('active').show(); 
    jQuery('.accordion .accordion-section-title').click(function(){ 
     if(jQuery(this).next().is(':hidden')) { 
      jQuery('.accordion .accordion-section-title').removeClass('active').next().slideUp(); 
      jQuery(this).toggleClass('active').next().slideDown(); 
     }else { 
     jQuery(this).removeClass('active').next().slideUp(); 
     } 
     return false; // Prevent the browser jump to the link anchor 
    }); 
});