使用jQuery更新href中的查询字符串值

我有3个具有不同结构的href,并且它们都具有相同的硬编码参数(在此示例中值= somevalue-abc).

如何更新链接并使用新值覆盖值?

jQuery的

$(document).ready(function () {

    $('a').click(function () {

        var $newValue = 'newvalue';

        //here a function that overwrites the parameter 'somevalue-abc' with $newValue

    });

});

HTML

<a class="demo" href="http://www.mydomain.com/page.aspx?demo&value=somevalue-abc">link 1</a>
<a class="real" href="http://www.mydomain.com/?value=somevalue-abc#go=yes">link 2</a>
<a class="outgoing" href="http://www.mydomain.com/page.aspx?value=somevalue-abc">link 3</a>

解决方法

尝试:

$(document).ready(function () {
    $('a').click(function () {
        var $newValue = 'newvalue';
        $(this).attr('href',$(this).attr('href').replace('somevalue-abc',$newValue));
    });
});

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...