简体   繁体   English

按钮onClick事件

[英]Button onClick event

For a study Project I try to make a sidebar for an interactive map. 对于研究项目,我尝试制作交互式地图的侧边栏。

I have a button which trigger an onClick() Event which Shows a <ul> with an form Input. 我有一个按钮,它触发一个onClick()事件,该事件显示带有形式Input的<ul>

So far I have 3 questions. 到目前为止,我有3个问题。

1) Can I check if the button was already clicked so I can hide my additional Content again? 1)我可以检查按钮是否已经被单击,以便可以再次隐藏其他内容吗? If so, how can I achieve that? 如果是这样,我该如何实现?

2) At the Moment I only display the content but I would like to have an Animation. 2)目前,我只显示内容,但我想制作一个动画。 Again, you may give me some Infos to do this or provide an link / tutorial. 同样,您可能会给我一些信息以执行此操作或提供链接/教程。

3) Is there a better Solution for my Problem, if so. 3)如果有,我的问题是否有更好的解决方案。 May you give me a hint? 你能给我一个提示吗?

 function showDropdown(x) { let id = x; if (id == 1) { document.getElementById("1").style.display = "block"; console.log(id); } if (id == 2) { document.getElementById("2").style.display = "block"; console.log(id); } if (id == 3) { document.getElementById("3").style.display = "block"; console.log(id); } else { console.log(id); } } 
 .nav-sidebar>ul { display: none; } 
 <div class="container-fluid"> <div class="row"> <div class="col-sm-3 col-md-2 sidebar"> <div class="nav nav-sidebar"> <button type="button" onclick="showDropdown(1)" class="dropdown-header">Einwohner</button> <ul class="dropdown-item" id="1"> <li> <p>Feld 1:<span style="padding-left: 10px;"><input type="text" Placeholder="a" size="2"</span></p> </li> <li> <p>Feld 2:<span style="padding-left: 10px;"><input type="text" Placeholder="b" size="2"</span></p> </li> <li> <p>Feld 3:<span style="padding-left: 10px;"><input type="text" Placeholder="c" size="2"</span></p> </li> </ul> </div> </div> 

You put jQuery in the tags, so I suppose you could use it, see this fiddle 您将jQuery放在了标签中,所以我想您可以使用它, 请看这个小提琴

You can use toggle() or slideToggle() function to show/hide the content. 您可以使用toggle()slideToggle()函数显示/隐藏内容。

$('button').click(function(){
    $('.dropdown-item').slideToggle();
});

You can achieve all of this using jQuery's .slideToggle() function. 您可以使用jQuery的.slideToggle()函数来实现所有这些功能。

function showDropdown(x) {
  let id = x;

  if (id == 1) {
    $("#1").slideToggle();
    console.log(id);
  }

  if (id == 2) {
     $("#2").slideToggle();
    console.log(id);
  }

  if (id == 3) {
    $("#3").slideToggle();
    console.log(id);
  } else {
    console.log(id);
  }
}

By using slideToggle you are checking to see what the current state is, toggling it to the opposite state and it will also animate. 通过使用slideToggle,您正在查看当前状态是什么,将其切换到相反的状态,它也会进行动画处理。

Edit; 编辑; Another user has beat me to it. 另一个用户击败了我。

Use a data attribute and jQuery slidetoggle as I suggested in my comment. 按照我在注释中的建议,使用数据属性和jQuery slidetoggle。

Please note I do not like numeric IDs so I prefixed the ID with a u. 请注意,我不喜欢数字ID,因此我在ID前面加上了u。

 $(function() { $(".dropdown-header").on("click", function() { var id = $(this).attr("data-id"); $("#" + id).slideToggle() console.log(id); }); }); 
 .nav-sidebar>ul { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container-fluid"> <div class="row"> <div class="col-sm-3 col-md-2 sidebar"> <div class="nav nav-sidebar"> <button type="button" data-id="u1" class="dropdown-header">Einwohner</button> <ul class="dropdown-item" id="u1"> <li> <p>Feld 1:&nbsp;<input type="text" Placeholder="a" size="2" /></p> </li> <li> <p>Feld 2:&nbsp;<input type="text" Placeholder="b" size="2" /></p> </li> <li> <p>Feld 3:&nbsp;<input type="text" Placeholder="c" size="2" /></p> </li> </ul> </div> </div> 

1- Check if button is clicked. 1-检查是否单击了按钮。

You can do this by assigning a variable i=0 outside, and when you perform onClick operation makeit i=1, and before you perform onclick operations, check if the value is 1 or 0. 您可以通过在外部分配变量i = 0来执行此操作,然后在执行onClick操作时使makeit i = 1,并且在执行onclick操作之前,请检查值是1还是0。

But there is another way of doing it using jquery. 但是还有另一种使用jquery的方法。

Hide and show elements using jquery 使用jQuery隐藏和显示元素

If you want to do it in little animation way. 如果您想以小动画的方式来做。 use fade function in Jquery 在jQuery中使用淡入淡出功能

Hide and show elements using fade() 使用fade()隐藏和显示元素

if you want tutorial on side navigation bar 如果您想要侧面导航栏上的教程

W3schools sidenav tutorial W3schools Sidenav教程

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

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