dongqian5569 2014-03-13 05:13
浏览 50
已采纳

单击相关按钮时如何隐藏相关div

I have a dynamic div's which needed to hide when relevant delete button clicked. This divs also have dynamic id's. i want to hide relevant div where button is clicked. im only getting the first div id all the time. Im new to jQuery can someone please help.

$(document).ready(function(){
  $(".reBox").click(function() {
    alert($('.reBox').attr('id'));
      // $(this).hide(); 
});
});

<div class="boxx" id="<?php echo $tot_meal; ?>">
  <div class="reBox" id="<?php echo $tot_meal; ?>">
     <img src="../images/error.png" width="16px" height="16px" />
  </div> 
</div>
  • 写回答

2条回答 默认 最新

  • dpt1712 2014-03-13 05:15
    关注

    Since you have dynamic elements, try event delegation

    $(document).on('click', ".reBox", function () {
        console.log(this.id);
        $(this).hide();
        //if you want to close boxx
        // $(this).closest('.boxx').hide();
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题