Php “保存唯一”;post“U id”;查找div

Php “保存唯一”;post“U id”;查找div,php,jquery,Php,Jquery,这是设置 <?php echo '<button type="button" class="rh_show_button" data-post_id="' . esc_attr( $post->id ) . '">' ;?> Setting <?php echo '</button>';?> <?php echo '<div class="rh_content" style="display:none;" data-pos

这是设置

<?php echo '<button type="button" class="rh_show_button" data-post_id="' . esc_attr( $post->id ) . '">' ;?>
    Setting
<?php echo '</button>';?>
<?php echo '<div class="rh_content" style="display:none;" data-post_id="' . esc_attr( $post->id ) . '">' ;?>
    Content
<?php echo '</button>';?>

<script>
 jQuery(".rh_content[data-post_id='" + jQuery(this).data("post_id") + "']").click(function(){
    jQuery(".rh_show_button[data-post_id='" + jQuery(this).data("post_id") + "").show();
  });     
</script>

背景
内容
jQuery(“.rh_content[data-post_id=”)+jQuery(this.data(“post_id”)+”])”)。单击(函数(){
jQuery(“.rh_show_按钮[data-post_id=”)+jQuery(this.data(“post_id”)+”).show();
});     
有一个按钮和一个隐藏的div

假设有10个按钮+内容,每个按钮都有唯一的post_id。 下面是我想做的

  • 单击
    按钮
    时,将保存唯一的
    post\u id
  • 保存的
    post\u id
    用于定位具有该post\u id的div,然后仅显示该特定div
  • 脚本是否正确?

    $(当您尝试在加载时绑定元素时,
    将不可用。请不要忘记将以下内容放入
    $(文档)。ready();

    答复评论


    jQuery(this)
    的值仅在绑定到元素的函数中可用。而
    if(post\u id.length>0)
    意味着具有class
    rh\u内容的单击元素中存在数据属性
    post\u id

    感谢您的回复。您能告诉我“if”是什么吗(post_id.length>0)是。您的意思是什么
    $(this)
    将不可用?谢谢:)@EmilyTurcato
    jQuery(this)
    的值将仅在绑定到元素的函数内部可用。如果
    if(post_id.length>0)
    表示类为
    rh\u内容的单击元素中存在数据属性
    post\u id
    jQuery(document).ready(function(){
        jQuery(".rh_content").click(function(){
            post_id = jQuery(this).data("post_id");
            if(post_id.length > 0)
               jQuery(".rh_show_button[data-post_id='" + jQuery(this).data("post_id") + "").show();
        });   
    })