简体   繁体   English

单击div中的按钮时关闭div

[英]close div when clicking button inside div

I have a div that loads information from another page when someone clicks on a thumbnail image on the same page. 我有一个div,当有人单击同一页面上的缩略图时,该div将从另一页面加载信息。

Currently, when a person clicks on a thumbnail image, the div #dynamic at the top of the page will load and display information from another page. 当前,当人们单击缩略图时,页面顶部的div #dynamic将加载并显示另一页面的信息。 I want to be able to allow users to click on a close button which i have given a class button_close for them to close the div instead of clicking anywhere within it. 我希望能够允许用户单击关闭按钮,该按钮已为我提供了一个class button_close类供他们关闭div,而不是单击其中的任何位置。 The button is being displayed within the #dynamic div as well. 该按钮也在#dynamic div中显示。

$(document).ready(function(){
   $('nav.main > a').click(function(e){
    e.preventDefault();
    $("#dynamic").load($(this).attr('href') + " .product_content", ()=>{ window.scrollTo(0, 0) }).fadeIn('slow');
   });
      $(".button_close").click(function(){
          $(".product_container").html ('');
      });
  });

Since your close_button is being generated dynamically, you can use the following 由于close_button是动态生成的,因此可以使用以下命令

$('body').on('click', '.button_close', function(){
    $('#dynamic').html('');
});

A simple example 一个简单的例子

<!DOCTYPE html>
<html>
<body>

<script src="http://code.jquery.com/jquery-latest.js"></script>

<script>
function loadDiv() {
    $('#dynamic').html('<p>Some content loaded from another page</p><div class="button_close">Close Me</div>');
}

$('body').on('click', '.button_close', function(){
    $('#dynamic').html('');
});
</script>

<div id="dynamic"></div><br><br>
<button onclick="loadDiv()">Load Div</button>

</body>
</html>

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

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