意外的按钮单击行为。

时间:2018-12-07 11:33:49

标签: javascript jquery html css click

在单击按钮#1时,按钮#2出现,并且按钮#2在单击按钮#1之后立即收到单击事件。

HTML:

module.exports = { X }

jQuery:

<button class="btn btn-primary default-btn" type="button">Ver no mapa</button> 

<button class="btn btn-primary button-maps display-none" type="button">
   <a href="https://goo.gl/maps/1HEDVJ2QHYr" target="_blank">
     <div class="btn-img-container">
      <img src="/unidades/PublishingImages/home-itaim/maps-icon.png" height="22" 
      width="22" alt="">
     </div>  
   </a> 
   <div class="divisor">
   </div>
   <a href="https://goo.gl/maps/1HEDVJ2QHYr" target="_blank">
     <div class="btn-img-container"> 
       <img src="/unidades/PublishingImages/home-itaim/waze-icon.png" 
       height="22" width="22" alt="">
      </div> 
    </a>
</button> 

1 个答案:

答案 0 :(得分:1)

我想你想要这个:-

您点击的方式错误。

我们可以通过这种方式点击.default-btn

('body').on("click", ".default-btn", function(){});

现在我们选择了.default-btn标签中的body类,然后运行函数。

  $("body").on("click", ".default-btn", function(){
    $(this).hide();
    $(this).parent().children(".button-maps").removeClass("display-none");
});
    
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  
  
<button class="btn btn-primary default-btn" type="button">Ver no mapa</button> 

<button class="btn btn-primary button-maps display-none" type="button">
   <a href="https://goo.gl/maps/1HEDVJ2QHYr" target="_blank">
     <div class="btn-img-container">
      <img src="/unidades/PublishingImages/home-itaim/maps-icon.png" height="22" 
      width="22" alt="">
     </div>  
   </a> 
   <div class="divisor">
   </div>
   <a href="https://goo.gl/maps/1HEDVJ2QHYr" target="_blank">
     <div class="btn-img-container"> 
       <img src="/unidades/PublishingImages/home-itaim/waze-icon.png" 
       height="22" width="22" alt="">
      </div> 
    </a>
</button>