Jquery 如何连接具有相同id的两个元素';s
我有一个wordpress自定义帖子类型,我已经像这样显示了它们Jquery 如何连接具有相同id的两个元素';s,jquery,wordpress,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(
<?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();?>
尝试:
同一页上的每个id都应该是唯一的,并且不应该以数字开头。 通常在这种情况下,我会使用以下方法:
<div id="i1" data-id="1">...</div>
<div id="j1">...</div>
您不应该有两个id相同的元素。代替id,我们给他们相同的类和用法。是的,我同意你@rohitara。是的,谢谢你的回答。我创建了类似的类和用法
$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();?>
$('.companyhistoryitem').on('click',function(){
$(this).parent().find('.company_historycontent').show();
//or $(this).next('.company_historycontent').show();
});
<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' );
});