2012-09-29 94 views
-1

有人可以幫我解決這個問題嗎?當我點擊「showfaqanswer」時,我只想要顯示「faqanswer」。我沒有看到PHP和jQuery代碼有什麼問題。謝謝您的幫助!jQuery的問題nextUntil

<?php query_posts("cat=17&posts_per_page=7&offset=7"); ?> 
<?php if (have_posts()) while (have_posts()) : the_post(); ?> 

<p> 
<a href="#" id="showfaqanswer"><?php the_title(); ?></a><br /> 
</p> 

<div id="faqanswer"><?php the_content(); ?></div> 

<?php endwhile; ?> 

jQuery(document).ready(function() { 
$('#showfaqanswer').click(function(){ 
$('#faqanswer').nextUntil('#showfaqanswer').show(); 
}); 
}); 
+0

ID必須是唯一的。更改ID上課,嘗試 – Kishore

回答

0

閱讀來自nextUntil選擇的documentation:它得到兄弟姐妹,所以不會選擇任何東西,如果它沒有兄弟姐妹。

$('#showfaqanswer').click(function(){ 

    // get the parent of the current #showfaqanswer, select until the the next p-tag 
    $(this).parent().nextUntil('p').show(); 
}); 
+0

上面的代碼似乎工作,但只有第一個元素。似乎suresh.g是正確的需要有獨特的ID? PHP代碼是一個WordPress循環,用於從某個類別獲取帖子 – cpcdev

+0

@cpcdev是的,絕對使用唯一的ID ...不知道爲什麼這不起作用,雖然 – McGarnagle

0

我們需要使用class。因爲ID必須是唯一的。

請試試這個:

<?php query_posts("cat=17&posts_per_page=7&offset=7"); ?> 
<?php $i=1; ?> 
<?php if (have_posts()) while (have_posts()) : the_post(); ?> 

<p> 
<a href="#" id="<?php echo $i;?>" class="fanswer"><?php the_title(); ?></a><br /> 
</p> 

<div id="faqanswer<?php echo $i;?>" style="display:none;"><?php the_content(); ?></div> 
<?php $i++; ?> 
<?php endwhile; ?> 

jQuery(document).ready(function() { 
$('.fanswer').click(function(){ 
var id = $(this).id; 
$('#faqanswer'+id).show(); 
}); 
}); 
+0

感謝您的回覆。我試過上面的代碼...但沒有運氣:(。其他建議? – cpcdev