简体   繁体   English

使用JavaScript根据URL动态更改href的值

[英]Dynamically change value on href according to URL with JavaScript

I'm looking for a way to dynamically change a value on a href according to the url. 我正在寻找一种根据URL动态更改href上的值的方法。 So for eg I have the url: testing.this.com?id=54908765 , I'd want to change all the links to have the same id value on the end ie this.url.com?id=54908765 . 因此,例如对于我有url: testing.this.com?id=54908765 id= this.url.com?id=54908765 ,我想更改所有链接,使其末尾具有相同的id值,即this.url.com?id=54908765

Is this possible just by using JavaScript? 仅使用JavaScript就能做到吗? I couldn't find any obvious solution when googling. 谷歌搜索时找不到任何明显的解决方案。 Thanks. 谢谢。

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var spslid = getParameterByName('id');
function change()
{
var z =  $("a") ;

for(int i=0; i<z.Count ; i ++)
{

z[i].attr("href" , z[i] +spslid);

}
}

change();

This is a approach 这是一种方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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