如何在jQuery中的某些锚元素中添加GET参数?

时间:2011-07-18 01:57:47

标签: javascript jquery

我有一堆a个元素,我想在他们的href属性中添加一个GET参数。

我想在适当的地方使用?&

例如,http://example.com应变为http://example.com?extra=truehttp://example.com?already_got_a_param=true应变为http://example.com?already_got_a_param=true&extra=true

什么代码会这样做?

1 个答案:

答案 0 :(得分:4)

你可以用这个......

$('a').attr('href', function(index, href) {
    return href + (this.search ? '&' : '?') + 'extra=true';
})

jsFiddle