-1

有人可以帮我解决这个问题吗?我只想在为每个点击“showfaqanswer”时显示“faqanswer”。我看不出 PHP 和 jQuery 代码有什么问题。谢谢您的帮助!

<?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();
});
});
4

2 回答 2

0

我们需要使用类。因为 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();
});
});
于 2012-09-29T06:00:48.343 回答
0

阅读选择器上的文档nextUntil:它获取兄弟姐妹,所以如果它没有兄弟姐妹,它不会选择任何东西。

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

    // get the parent of the current #showfaqanswer, select until the the next p-tag
    $(this).parent().nextUntil('p').show();
});
于 2012-09-29T05:50:17.683 回答