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条)

相关推荐 更多相似问题