我有 3 个具有不同结构的 href,并且它们都具有相同的硬编码参数(在此示例中 value=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));
});
});
关于jquery - 使用 jQuery 更新 href 中的查询字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7495470/