简体   繁体   English

如何根据jQuery中的表格行按钮位置打开UL?

[英]How to open a UL as per table row button position in jQuery?

I have a table right now there are three rows with Add Section button (it may be increase).我现在有一个表,有三行带有“ Add Section按钮(可能会增加)。 When i click on Add Section a UL is toggle, its working fine just for first row Add Section , when i click on second button then UL open with top button.当我点击Add SectionUL被切换,它只对第一行Add Section工作正常,当我点击第二个按钮时, UL用顶部按钮打开。 how can i fix it?我该如何解决?

In simple way, i want open UL as per button position.以简单的方式,我想根据按钮位置打开UL how can i do this with single UL list?我如何使用单个UL列表执行此操作?

My Code:-我的代码:-

 $(function(){ $('.add-section').click(function(){ $('.section-area').toggle(); }); })
 .section-area{ background:#fff; padding: 5px; border-radius: 4px; box-shadow: 2px 4px 10px rgba(39, 59, 69, 0.4); width: 100px; position: absolute; top:61px; right:140px; display: none;}
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script> <table class="table table-bordered"> <thead> <tr> <th>Firstname</th> <th>Lastname</th> <th>Email</th> <th>Action</th> </tr> </thead> <tbody> <tr> <td>John</td> <td>Doe</td> <td>john@example.com</td> <td><button class="add-section btn btn-primary">Add Section</button></td> </tr> <tr> <td>Mary</td> <td>Moe</td> <td>mary@example.com</td> <td><button class="add-section btn btn-primary">Add Section</button></td> </tr> <tr> <td>July</td> <td>Dooley</td> <td>july@example.com</td> <td><button class="add-section btn btn-primary">Add Section</button></td> </tr> </tbody> </table> <div class="section-area"> <ul> <li>Add 1</li> <li>Add 2</li> <li>Add 3</li> </ul> </div>

Answer will be appreciated!答案将不胜感激!

Your problem occurs because the top and right of your section-area are fixed.出现您的问题是因为您的section-areatopright是固定的。 To fix that, simply change it so that the top and right change in accordance to which button is clicked.要解决这个问题,只需更改它,以便根据单击的按钮更改topright Here is a working example:这是一个工作示例:

 $(function(){ $('.add-section').click(function(){ let buttonTopPosition = $(this).offset().top let buttonLeftPosition = $(this).offset().left let sectionAreaWidth = $(this).outerWidth() $('.section-area').toggle(); $('.section-area').css({top: buttonTopPosition, left: buttonLeftPosition - sectionAreaWidth}) }); })
 .section-area{ background:#fff; padding: 5px; border-radius: 4px; box-shadow: 2px 4px 10px rgba(39, 59, 69, 0.4); width: 100px; position: absolute; top: 0; left: 0; display: none;}
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script> <table class="table table-bordered"> <thead> <tr> <th>Firstname</th> <th>Lastname</th> <th>Email</th> <th>Action</th> </tr> </thead> <tbody> <tr> <td>John</td> <td>Doe</td> <td>john@example.com</td> <td><button class="add-section btn btn-primary">Add Section</button></td> </tr> <tr> <td>Mary</td> <td>Moe</td> <td>mary@example.com</td> <td><button class="add-section btn btn-primary">Add Section</button></td> </tr> <tr> <td>July</td> <td>Dooley</td> <td>july@example.com</td> <td><button class="add-section btn btn-primary">Add Section</button></td> </tr> </tbody> </table> <div class="section-area"> <ul> <li>Add 1</li> <li>Add 2</li> <li>Add 3</li> </ul> </div>

I changed your CSS top and left to be initially 0 .我更改了您的 CSS topleft为最初的0 Then, I will update those properties when a button is clicked (updated using jQuery).然后,我将在单击按钮时更新这些属性(使用 jQuery 更新)。

I advise you to use the bootstrap dropdown for so many reasons:我建议您使用 bootstrap 下拉菜单的原因有很多:

  1. your code is not well maintained for ex.你的代码维护得不好。 when you click on the second row you if the dropdown is already opened you have to click two times.当您单击第二行时,如果下拉列表已打开,则必须单击两次。
  2. there no indication that this is a drop down.没有迹象表明这是一个下拉。
  3. styles are not well prepared.样式没有准备好。

You don't need to reinvent the wheel you are using the bootstrap so try to use it as much as you can.您不需要重新发明正在使用引导程序的轮子,因此请尽可能多地使用它。

Check the snippet:检查片段:

 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script> <table class="table table-bordered"> <thead> <tr> <th>Firstname</th> <th>Lastname</th> <th>Email</th> <th>Action</th> </tr> </thead> <tbody> <tr> <td>John</td> <td>Doe</td> <td>john@example.com</td> <td> <div class="dropdown"> <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> Add Section </button> <div class="dropdown-menu" aria-labelledby="dropdownMenuButton"> <a class="dropdown-item" href="#">Action</a> <a class="dropdown-item" href="#">Another action</a> </div> </div> </td> </tr> <tr> <td>Mary</td> <td>Moe</td> <td>mary@example.com</td> <td> <div class="dropdown"> <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> Add Section </button> <div class="dropdown-menu" aria-labelledby="dropdownMenuButton"> <a class="dropdown-item" href="#">Action</a> <a class="dropdown-item" href="#">Another action</a> </div> </div> </td> </tr> <tr> <td>July</td> <td>Dooley</td> <td>july@example.com</td> <td> <div class="dropdown"> <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> Add Section </button> <div class="dropdown-menu" aria-labelledby="dropdownMenuButton"> <a class="dropdown-item" href="#">Action</a> <a class="dropdown-item" href="#">Another action</a> </div> </div> </td> </tr> </tbody> </table>

喜欢就点个赞

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

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