简体   繁体   中英

Add href to anchor tag on the n click

<section>
        <a **id="download"**  oncontextmenu="return false" > <svg ...
          </svg></a>
</section>
<script type="text/javascript">

The Code Below is just to get first 2 clicks.

$("#download").click(function(){
  $("#download").attr("oncontextmenu", "false");
});

$("#download").click(function({
  $("#download").attr("oncontextmenu", "false");
});

Here is the finalfunction which adds HREF on the 3rd click.

$("#download").click(finalfunction({
$("#download").attr("href", "<%= downloadLink %>");
});

</script>

But this code isn't working. Help me please.

  1. IDs need to be unique. Use a class.

  2. Have ONE eventListener and count the clicks inside it

Try something like this

I added some debugging

 $(".download").on("click", e => { const anchor = e.currentTarget; let cnt = anchor.dataset.cnt; if (cnt === "2") { anchor.href = anchor.dataset.href; console.log("clicked 3 times") } else { cnt++; anchor.dataset.cnt = cnt; $(".count",anchor).html(`Click ${3-cnt} more time${3-cnt === 1? "": "s"}`) e.preventDefault() } })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <section> <a class="download" href="#" data-cnt="0" data-href="...." class oncontextmenu="return false"><span class="count">Click 3 times</span> <svg... </svg></a> <a class="download" href="#" data-cnt="0" data-href="...." class oncontextmenu="return false"><span class="count">Click 3 times</span> <svg... </svg></a> </section>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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