简体   繁体   English

单击relevent按钮时如何隐藏relevent div

[英]How to hide relevent div when relevent button is clicked

I have a dynamic div's which needed to hide when relevant delete button clicked. 我有一个动态div,需要在单击相关的删除按钮时隐藏它。 This divs also have dynamic id's. 该div也具有动态ID。 i want to hide relevant div where button is clicked. 我想在单击按钮的地方隐藏相关的div。 im only getting the first div id all the time. 我只是一直获取第一个div ID。 Im new to jQuery can someone please help. 我是jQuery的新手,请有人帮忙。

$(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>

As you have dynamic html elements use .on() . 当您具有动态html元素时,请使用.on() Try this: 尝试这个:

$(document).ready(function(){
  $(".boxx").on('click','.reBox',(function() {
      $(this).hide(); //to hide reBox
      $(this).parent("div.boxx").hide(); //to hide boxx element
});
});

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();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM