如何连接具有相同ID

时间:2015-11-13 07:41:59

标签: jquery wordpress

我有一个wordpress自定义帖子类型,我已经像这样显示了它们

<?php
            $count = 1;$counter = 1;
            $query = new WP_Query( array('post_type' => 'companyhistory', 'posts_per_page' => -1, 'order' => 'ASC' ) );
            while ( $query->have_posts() ) : $query->the_post(); 

        ?>
        <div class="companyhistoryitem history_year<?php echo $count;?>" id="<?php echo $count;?>"><?php the_title();?></div>
        <div class="company_historycontent history_year<?php echo $count;?>" id="<?php echo $counter;?>"><?php the_content();?></div>
            <?php $count++;$counter++; endwhile; wp_reset_query();?> 

我想在点击.companyhistoryitem时显示已被证实的内容。我想用这个jquery代码连接它们但没有运气

jQuery('.companyhistoryitem').each(function(){
    jQuery(this).click(function(){
        var historytitleid = jQuery(this).attr('id');
        jQuery('.company_historycontent').each(function(){
            var historycontentid = jQuery(this).attr('id');
            console.log(historycontentid);
        });
        var aaa  = jQuery('.company_history_section .maincontainer').find('.company_historycontent').attr('id' , historytitleid);
        if(historytitleid == aaa){
            console.log(aaa.html());
        }
    });
});

2 个答案:

答案 0 :(得分:2)

TRY:

    $query = new WP_Query( array('post_type' => 'companyhistory', 'posts_per_page' => -1, 'order' => 'ASC' ) );
    while ( $query->have_posts() ) : $query->the_post(); 

?>
<div class="parent">
<div class="companyhistoryitem history_year"><?php the_title();?></div>
<div class="company_historycontent history_year"><?php the_content();?></div></div>
    <?php endwhile; wp_reset_query();?> 

JS:

$('.companyhistoryitem').on('click',function(){
  $(this).parent().find('.company_historycontent').show(); 
  //or $(this).next('.company_historycontent').show(); 
});

答案 1 :(得分:1)

每个ID在同一页面上应该是唯一的,并且不应以数字开头。 通常在这种情况下我使用类似的东西:

<div id="i1" data-id="1">...</div>
<div id="j1">...</div>

然后:

$('#i1').click( function() {
  var id = $(this).attr( 'data-id' );
  $('#j'+id).css( 'color', 'red' );
});