简体   繁体   English

单击时隐藏div-多个框

[英]Hide divs on click - multiple boxes

I have a bit long info submission form. 我的信息提交表格有点长。 I have an option that someone click "add another", there is a notification box display with text like "Success!" 我可以选择某个人单击“添加另一个”,然后显示一个通知框,其中显示诸如“成功!”之类的文字。 like that with close button. 像这样用关闭按钮。 Please check this image. 请检查这张图片。

在此处输入图片说明

Therefore I have three "add another" buttons and three success alerts. 因此,我有三个“添加另一个”按钮和三个成功警报。 When someone click close, that alert message needs to be disappear. 当有人单击关闭时,该警告消息需要消失。

The problem 问题

I used javascript for that success box. 我使用javascript作为成功框。 It does these things. 它做这些事情。

Click "Add another" button, appears success box. 单击“添加另一个”按钮,出现成功框。
Click close button on success box, box disappears. 单击成功框上的关闭按钮,框消失。

Here is my code. 这是我的代码。

 document.querySelector(".add-box").addEventListener("click", function() { document.querySelector(".add-box-wrap").style.display = "inline-block"; }); document.querySelector(".add-box1").addEventListener("click", function() { document.querySelector(".add-box-wrap1").style.display = "inline-block"; }); document.querySelector(".add-box2").addEventListener("click", function() { document.querySelector(".add-box-wrap2").style.display = "inline-block"; }); $(".claim-btn-close").click(function() { $(".add-box-wrap").hide(); }); $(".claim-btn-close1").click(function() { $(".add-box-wrap1").hide(); }); $(".claim-btn-close2").click(function() { $(".add-box-wrap2").hide(); }); <!-- This goes end of html page --> function hide(target) { document.getElementsByClassName(target).style.display = 'none'; }; 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <!--Success form--> <div class="add-box-wrap"> <div class="claim-btn-close" onclick="hide('.claim-btn-close')"> <i class="fa fa-times" aria-hidden="true"></i> </div> <a href="" class="btn btn-primary claim-btn">Success!</a> </div> <!--Add another button --> <div class="col-lg-10 col-md-10 col-sm-12 no-padding"> <a class="add-box">Add another<i class="fa fa-plus"></i></a> </div> 

See.. there is a major problem with code repeating. 参见..代码重复存在一个主要问题。 (I didn't repeat html codes, just post here one example) I would like to know the best practice to achieve things like this. (我没有重复html代码,只是在此处发布了一个示例)我想知道实现此类目标的最佳做法。

You can use DOM relationship to target the desired element. 您可以使用DOM关系来定位所需的元素。 Bind event handlers using a common class then use .closest() to traverse up desired element. 使用公共类绑定事件处理程序,然后使用.closest()遍历所需的元素。

$(".claim-btn-close").click(function(){
    $(this).closest(".add-box-wrap").hide();
});

If element are dynamically generate use Event Delegation approach. 如果元素是动态生成的,则使用事件委托方法。

$(staticParentElemet).on('click',".claim-btn-close",function(){
    $(this).closest(".add-box-wrap").hide();
});

For displaying the notification box without repeating the code, one of the way is you could make use of data attributes. 为了在不重复代码的情况下显示通知框,一种方法是可以利用data属性。 Change the data attribute for all the buttons and let the class .add-box be the same. 更改所有按钮的data属性,并让类.add-box相同。 like so: 像这样:

<div class="add-box-wrap1" style="display: none;">
  <div class="claim-btn-close">
    <i class="fa fa-times" aria-hidden="true"></i>
  </div>
  <a href="" class="btn btn-primary claim-btn">Success!</a>
</div>
<div class="add-box-wrap2" style="display: none;">
  <div class="claim-btn-close">
    <i class="fa fa-times" aria-hidden="true"></i>
  </div>
  <a href="" class="btn btn-primary claim-btn">Success!</a>
</div>

<!--Add another button -->
<div class="col-lg-10 col-md-10 col-sm-12 no-padding">
  <a class="add-box" data-target="1">Add another<i class="fa fa-plus"></i></a>
</div>

<!--Add another button -->
<div class="col-lg-10 col-md-10 col-sm-12 no-padding">
  <a class="add-box" data-target="2">Add another<i class="fa fa-plus"></i></a>
</div>

And change your Jquery to just one function . 并将您的Jquery更改为仅一个函数

$(".add-box").click(function() {
  $(".add-box-wrap" + $(this).data('target')).show(); //.add-box-wrap1 or .add-box-wrap2 
});

Explanation of the above line: It just means we are getting the value of the "clicked" button's data-target value(which is 1 or 2) AND appending that value to the "add-box-wrap"(which will be add-box-wrap1 or add-box-wrap2 ) to search for that element to display it using .show() . 上一行的说明:这仅表示我们正在获取“被点击”按钮的data-target (为1或2)的值,并将该值附加到“ add-box-wrap”(将为add-box-wrap1add-box-wrap2 ),以使用.show()搜索该元素以显示该元素。

Using show() or .css jquery functions is upto you. 您可以使用show().css jquery函数。 But I suggest not to mix both vanilla JS and JQuery codes(like in your question). 但是我建议不要混合使用普通的JS和JQuery代码(就像您的问题一样)。

Closing 闭幕

You can simply use closest or parent , like so: 您可以简单地使用closestparent ,如下所示:

$(".claim-btn-close").click(function(){
    $(this).parent().hide();
});

 $(".add-box").click(function() { $(".add-box-wrap" + $(this).data('target')).show(); }); $(".claim-btn-close").click(function(){ $(this).parent().hide(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="add-box-wrap1" style="display: none;"> <div class="claim-btn-close"> <i class="fa fa-times" aria-hidden="true"></i> </div> <a href="" class="btn btn-primary claim-btn">Success!</a> </div> <div class="add-box-wrap2" style="display: none;"> <div class="claim-btn-close"> <i class="fa fa-times" aria-hidden="true"></i> </div> <a href="" class="btn btn-primary claim-btn">Success!</a> </div> <!--Add another button --> <div class="col-lg-10 col-md-10 col-sm-12 no-padding"> <a class="add-box" data-target="1">Add another<i class="fa fa-plus"></i></a> </div> <!--Add another button --> <div class="col-lg-10 col-md-10 col-sm-12 no-padding"> <a class="add-box" data-target="2">Add another<i class="fa fa-plus"></i></a> </div> 

Update on another question(3 column): 更新另一个问题(第3栏):

Using bootstrap, you can wrap the add-box-wrap divs within a .row and give each of it a class like col-*-4 ( class="col-xs-4 col-md-4 col-lg-4" ) assuming you have 3 success buttons(12/3 = 4). 使用引导程序,您可以将add-box-wrap div add-box-wrap.row并为每个类提供一个类似于col-*-4的 class="col-xs-4 col-md-4 col-lg-4"class="col-xs-4 col-md-4 col-lg-4" ),假设您有3个成功按钮(12/3 = 4)。 So the first part of your html would become 因此,您的html的第一部分将成为

<div class="row">
  <div class="add-box-wrap1 col-md-4 col-lg-4" style="display: none;">
    <div class="claim-btn-close">
      <i class="fa fa-times" aria-hidden="true"></i>
    </div>
    <a href="" class="btn btn-primary claim-btn">Success!</a>
  </div>
  <div class="add-box-wrap2 col-md-4 col-lg-4" style="display: none;">
    <div class="claim-btn-close">
      <i class="fa fa-times" aria-hidden="true"></i>
    </div>
    <a href="" class="btn btn-primary claim-btn">Success!</a>
  </div>
  <div class="add-box-wrap3 col-md-4 col-lg-4" style="display: none;">
    <div class="claim-btn-close">
      <i class="fa fa-times" aria-hidden="true"></i>
    </div>
    <a href="" class="btn btn-primary claim-btn">Success!</a>
  </div>
</div>

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

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