dsvs50005 2014-05-13 16:40
浏览 77
已采纳

仅在单击锚点时生成URL

Is it possible to assign url to the an anchor only when it got clicked?

<a href="http://example.com/">Token Link</a>

When the anchor got clicked, it will go to http://example.com/token=xxxxx/

I want to generate token only when it got clicked.

If possible, How?

thanks

  • 写回答

2条回答 默认 最新

  • dsfsda121545 2014-05-13 17:20
    关注

    Opening the link in another window using jQuery

    $(document).ready(function () {
      $(".thisClass a").on("click", function(e){
        e.preventDefault(); // this prevents going to the original url or default behavior
        var changedLink = $(this).attr("href", $(this).attr("href") + "/token=xxxx");
        var newUrl = $(changedLink).attr('href');
        window.open(newUrl, '_blank');
    
      });
    
    });
    

    // Here is a way to do it with Plain Javascript - i did not test it on all browsers but worked with chrome for example.

    // goes in a script.js or in script tags under the </body> element
    function changeTheLink() {
      event.preventDefault();
      var aLink = document.getElementById('theLink');
      var theOldLink = aLink.getAttribute("href");
      aLink.setAttribute('href', theOldLink + "/token=xxxx");
      var theNewLink = aLink.getAttribute("href");
      window.open(theNewLink, "_blank");
    
    }
    // here is the HTML you owuld have to add an id and an onclick attribute to use this code
    
     <div class="thisClass"><a href="http://thiswebsite.com" id="theLink"
     onclick="changeTheLink()">Here is a link</a></div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题