如何在jquery中处理父id中的元素
我想在增加id(#showcp)中显示内容,当单击id(#优惠券)时,我使用php增加id的值,我只想在父id(#cp)框中显示内容,如何解决这个问题如何在jquery中处理父id中的元素,jquery,Jquery,我想在增加id(#showcp)中显示内容,当单击id(#优惠券)时,我使用php增加id的值,我只想在父id(#cp)框中显示内容,如何解决这个问题 <div class="row cp_box" > <?php $counter = -1; $args = array( 'post_status' => 'publish', 'post_type' => 'post', 'showpost
<div class="row cp_box" >
<?php
$counter = -1;
$args = array(
'post_status' => 'publish',
'post_type' => 'post',
'showposts' => 3,
'cat' => 6,
);
?>
<?php $getposts = new WP_query($args); ?>
<?php global $wp_query; $wp_query->in_the_loop = true; ?>
<?php while ($getposts->have_posts()) : $getposts->the_post(); $counter++ ?>
<div id="cp_box_<?php echo $counter; ?>" class="col large-4" style="background-image:url(<?php the_post_thumbnail_url(); ?>);position:relative;">
<ul class="time_cp">
<li class="time-begin">Time begin: </li>
<li class="time-end">Expired: </li>
</ul>
<div class="coupon_wrap">
<a id="coupon_click_<?php echo $counter; ?>" href="#" target="_blank">View coupon</a>
<p id="showcp_<?php echo $counter; ?>" class="cp_show" style="display:none;">Coupon code</p>
<a class="readmore" href="<?php the_permalink(); ?>">View detail</a>
</div>
</div>
<?php endwhile; wp_reset_postdata(); ?>
</div>
<script>
$(document).ready(function() {
$("[id^='coupon_click_']").click(function() {
$("[id^='showcp_']").show();
$("[id^='coupon_click_']").hide();
});
});
</script>
您可以使用正确的this
上下文轻松解决此问题,如:
$(document).ready(function() {
$("[id^='coupon_click_']").click(function() {
$(this).closest(".coupon_wrap").find("[id^='showcp_']").show();
$(this).hide();
});
});
说明:
$(此).closest(“.coupon\u wrap”)
将找到类为.coupon\u wrap
的最接近的父div,指向单击的优惠券单击链接
- 然后,它将在该div中找到
showcp\uu
元素并显示它
- 最后使用
$(this).hide()
我们只需隐藏单击的优惠券\u单击
元素,而不是隐藏id以优惠券\u单击
开头的所有元素
您可以使用正确的此
上下文轻松解决此问题,如:
$(document).ready(function() {
$("[id^='coupon_click_']").click(function() {
$(this).closest(".coupon_wrap").find("[id^='showcp_']").show();
$(this).hide();
});
});
说明:
$(此).closest(“.coupon\u wrap”)
将找到类为.coupon\u wrap
的最接近的父div,指向单击的优惠券单击链接
- 然后,它将在该div中找到
showcp\uu
元素并显示它
- 最后使用
$(this).hide()
我们只需隐藏单击的优惠券\u单击
元素,而不是隐藏id以优惠券\u单击
开头的所有元素
请考虑在5分钟后标记答案以帮助将来的用户。谢谢请考虑将答案标记为5分钟后,以帮助未来的用户。谢谢