简体   繁体   English

jQuery的停止点击事件

[英]JQuery Stop a click event

While adding a id attribute to a tag the event associated with it runs automatically. 同时增加了id属性, a标签与它相关的事件自动运行。 How to stop that from happening. 如何阻止这种情况的发生。

There is a tag with .show-all class when I click on that I am adding a id #hide-all .Now I want that if I click on that id the data should hide but the problem is it is running on click of .show-all . 单击时,有a带有.show-alla标记,我正在添加一个ID #hide-all 。现在,我希望如果单击该ID,则数据应隐藏,但问题是单击时运行.show-all

FIDDLE 小提琴

Here is the code: 这是代码:

 $(".show-all").click(function(e) { $(".data").show(); $(this).text("Hide-All"); $(this).removeClass("show-all"); $(this).attr('id', "hide-all"); }); $(document).on('click', "#hide-all", function(e) { $(".data").hide(); $(this).text("Show-All"); $(this).removeAttr("id"); $(this).addClass("show-all"); }); 
 .data { display: none; } a { text-decoration: none; color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="cont"> <a class="show-all">Show all</a> <div class="data"> <p> A1 </p> <p> A2 </p> <p> A3 </p> <p> A4 </p> </div> </div> 

I would just do both bindings on the same element as it is the same thing you will be clicking to do the show and hide: 我只是将两个绑定都放在同一个元素上,因为这是您要单击以显示和隐藏的同一件事:

 $(".show-all").click(function(e) { var link = $(this); // cache this for better performance if (link.text() === 'Show all') { $(".data").show(); link.text("Hide all") .removeClass("show-all") // you can chain these, you don't need to put $(this) before each .attr('id', "hide-all"); // don't know if you still need this } else { $(".data").hide(); link.text("Show all") .removeAttr("id") .addClass("show-all"); } }); 
 .data { display: none; } a { text-decoration: none; color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="cont"> <a class="show-all">Show all</a> <div class="data"> <p> A1 </p> <p> A2 </p> <p> A3 </p> <p> A4 </p> </div> </div> 

Update 更新资料

As you have said that this is only an example and you would need to rebind - the only way to do this is unbind previous events or use .one so you only bind one click event and rebind a new event at the point of clicking: 正如您所说的,这只是一个示例,您将需要重新绑定-唯一的方法是取消绑定先前的事件或使用.one因此您仅绑定一个click事件并在单击时重新绑定一个新事件:

 bindShow(); function bindShow() { $(".show-all").one('click', function(e) { // only bind one click - as it will change to a hide once clicked $(".data").show(); $(this) .text("Hide all") .removeClass("show-all") .attr('id', "hide-all"); bindHide(); }); } function bindHide() { $("#hide-all").one('click', function(e) { $(".data").hide(); $(this) .text("Show all") .removeAttr("id") .addClass("show-all"); bindShow(); }); } 
 .data { display: none; } a { text-decoration: none; color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="cont"> <a class="show-all">Show all</a> <div class="data"> <p> A1 </p> <p> A2 </p> <p> A3 </p> <p> A4 </p> </div> </div> 

If you just want to hide/show your <div class="data"> you can just use .toggle() and compare the text content of the .show-all class 如果您只想隐藏/显示<div class="data"> ,则可以使用.toggle()并比较.show-all类的文本内容

 $(".show-all").click(function(e) { $(".data").toggle(); if ($(this).text() == "Show-All"){ $(this).text("Hide-All") } else if ($(this).text() == "Hide-All"){ $(this).text("Show-All") } }); 
 .data { display: none; } a { text-decoration: none; color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="cont"> <a class="show-all">Show-All</a> <div class="data"> <p> A1 </p> <p> A2 </p> <p> A3 </p> <p> A4 </p> </div> </div> 

I think you are looking for a toogle function: 我认为您正在寻找功能:

$('.toggle).on('click', function(e) {
    $(".data").toggle();
});

EDIT: 编辑:

The problem is that you have already attached the event to the element. 问题在于您已经将事件附加到了元素上。 Even if you then change the class/id of the element, the event will stay attached. 即使您随后更改了元素的类/ id,事件也将保持连接状态。

But you could store the current status in a variable: 但是您可以将当前状态存储在变量中:

  var status = 0
  $(".btn").on("click", function() {
    if (status === 0) {
      $(this).html("Send Event 2");
      $("#output").html("Received Event 1");
      status = 1;
    } else {
      $(this).html("Send Event 1");
      $("#output").html("Received Event 2");
      status = 0;
    }
  });

See this Plunker: https://plnkr.co/edit/bEn4q0RhrotEuo3RoaDa?p=preview 查看此Plunker: https ://plnkr.co/edit/bEn4q0RhrotEuo3RoaDa ? p = preview

If you don't like the idea with the variable you can set a flag in the the data attribute of the link: 如果您不喜欢使用变量的想法,可以在链接的data属性中设置一个标志:

<a href="#" class=".btn" data-status="0"> 

You can then change the data-status attribute with jQuery on each click. 然后,您可以在每次单击时使用jQuery更改data-status属性。

Here you are https://jsfiddle.net/rp5teg49/3/ 这是https://jsfiddle.net/rp5teg49/3/

 $(".show-all").click(function(e) { $(".data").toggle('fast'); if ($(this).text() === "Hide-All") { $(this).text("Show-All"); } else { $(this).text("Hide-All"); } }); 
 .data { display: none; } a { text-decoration: none; color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="cont"> <a class="show-all">Show all</a> <div class="data"> <p> A1 </p> <p> A2 </p> <p> A3 </p> <p> A4 </p> </div> </div> 

Because when you add event, 2 events are call 因为添加事件时会调用2个事件

$(".data").hide(); 
$(".data").show(); 

You can console log in 2 event and see what happend 您可以控制台登录2事件并查看发生了什么

Try this... 尝试这个...

 $(document).on('click',".show-all",function(e) { $(".data").show(); $(this).text("Hide-All"); $(this).removeClass("show-all"); $(this).attr('id', "hide-all"); }); $(document).on('click', "#hide-all", function(e) { $(".data").hide(); $(this).text("Show-All"); $(this).removeAttr("id"); $(this).addClass("show-all"); }); 
 .data { display: none; } a { text-decoration: none; color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="cont"> <a class="show-all">Show all</a> <div class="data"> <p> A1 </p> <p> A2 </p> <p> A3 </p> <p> A4 </p> </div> </div> 

 $(".show-all").click(function(e) { if( $(this).attr('id')!='hide-all'){ $(this).text("Hide-All"); $(this).removeClass("show-all"); $(this).attr('id', "hide-all"); $(".data").show(); } else{ $(".data").hide(); $(this).text("Show-All"); $(this).removeAttr("id"); $(this).addClass("show-all"); } }); 
 .data { display: none; } a { text-decoration: none; color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="cont"> <a class="show-all">Show all</a> <div class="data"> <p> A1 </p> <p> A2 </p> <p> A3 </p> <p> A4 </p> </div> </div> 

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

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