简体   繁体   English

如果连续两次单击某个元素,则隐藏div,否则显示

[英]Hide a div if an element has been clicked twice in a row, else Show

I have several items in my navigation bar, and a div next to it, ie: 我的导航栏中有几个项目,旁边有一个div ,即:

<nav>
  <a id="a"></a>
  <a id="b"></a>
  <a id="c"></a>
</nav>
<div id="menu-col"></div>

If the same link is clicked twice in a row, I want to hide #menu-col . 如果连续两次点击相同的链接,我想隐藏#menu-col If not, I want #menu-col to remain visible. 如果没有,我希望#menu-col保持可见。

I'm not a javascript guy so I tried this: 我不是一个javascript的家伙所以我试过这个:

var lastClicked;
$('nav a').on('click', function(e) {
    alert(e.target.id + " - " + this.lastClicked);
    if (e.target.id == this.lastClicked) {
        $('#menu-col').hide();
        this.lastClicked = '';
    }
    else {
        $('#menu-col').show();
        this.lastClicked = e.target.id;
    }
});

Then I remembered that javascript assigns references, and not values. 然后我记得javascript分配引用,而不是值。 So when I did this.lastClicked = e.target.id; 所以当我这样做时this.lastClicked = e.target.id; I'm assigning a reference to my element's id, then on the next click I make that e.target.id == '' . 我正在为我的元素的id分配一个引用,然后在下一次单击时我创建了e.target.id == ''

In javascript, what would be the proper way of closing a box if the same link is clicked twice, and if not making sure the box is visible. 在javascript中,如果同一个链接被点击两次,如果没有确保该框是可见的,那么关闭框的正确方法是什么。

You can achieve this using toggleClass() to set a state class on the clicked a and also using toggle() on the .menu-col to show or hide it based on that state class. 您可以使用toggleClass()在单击的a上设置一个状态类,并使用.menu-col上的toggle()来显示或隐藏它基于该状态类。 Try this: 尝试这个:

 $('nav a').click(function(e) { e.preventDefault(); var $a = $(this); $a.toggleClass('active').siblings().removeClass('active'); $('.menu-col').toggle($a.hasClass('active')); }); 
 .menu-col { display: none; } .active { color: #C00; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <nav> <a id="a" href="#">a</a> <a id="b" href="#">b</a> <a id="c" href="#">c</a> </nav> <div class="menu-col">menu-col</div> 

As long as you keep those ids unique across you app (which you should be doing anyway) the approach you've chosen isn't wrong. 只要你在你的应用程序中保持这些ID是独一无二的(无论如何你都应该这样做),你所选择的方法并没有错。 Any primitive in javascript is actually stored by value. javascript中的任何原语实际上都是按值存储的。 Those primitives are string, number, boolean, and symbol. 这些原语是字符串,数字,布尔值和符号。 For more info see here https://developer.mozilla.org/en-US/docs/Glossary/Primitive 有关详细信息,请参阅https://developer.mozilla.org/en-US/docs/Glossary/Primitive

I would suggest something like this, you should have some kind of condition in which the div shows after it has been hidden. 我会建议像这样的东西,你应该有一些条件,其中div显示隐藏后。

$('nav a').dblclick(function(event) {
    event.preventDefualt();
    alert($(this).attr('id'));
    $('#menu-col').toggle();
});

Something like that should be exactly what you are looking for, like I said though, there should be a condition in which it shows itself again, I made it a toggle so any double click on any 'nav a' element will cause it to show/hide the div. 这样的东西应该是你正在寻找的东西,就像我说的那样,应该有一个条件,它再次显示自己,我做了一个切换所以任何双击任何'nav a'元素将导致它显示/隐藏div。

Just for the sake of an option. 只是为了一个选择。 Here is another way for double clicks(clicked twice in a row). 这是双击的另一种方法(连续点击两次)。

Using ondblclick event. 使用ondblclick事件。

 <a href="#" ondblclick="console.log('Double clicked!');">Double-click me.</a> 

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

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