Add Query-String parameter to static link on click

后端 未结 2 1333
南笙
南笙 2021-02-14 14:28

I\'m looking for a robust way of dynamically injecting a query string parameter when a visitor clicks on a static anchor-link.

For example a link:



        
2条回答
  •  别跟我提以往
    2021-02-14 15:14

    If you already know the value to append when loading the page then you can add this when the document is ready : If you're using JQuery use this:

    $( 'class' ).attr( 'href', function(index, value) {
    
    
    return value + '?item=myValue';
    });
    

    Otherwise (plain Javascript):

    var link = document.getElementById("mylink");
    link.setAttribute('href', 'http://www.johndoeslink.com'+'?item=myValue');
    

提交回复
热议问题