event.preventDefault() on first click then remove

后端 未结 5 1449
余生分开走
余生分开走 2021-02-03 13:35

I have the default anchor disabled if it has a class subnav as shown in this fiddle.

I only want this disabled for the first click then I want the normal anchor function

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-03 14:18

    Bind the event handler with one()docu. It executes once and automatically unbinds itself afterwards.

    $(".subnav a").one("click", function(event) {
      event.preventDefault();
    });
    

    Alternatively you can unbind it yourself directly in the function. It's good to use a namespace for that

    $(".subnav a").bind("click.myclick", function(event) {
      event.preventDefault();
      $(this).unbind(".myclick");
    });
    

提交回复
热议问题