我有一堆a
个元素,我想在他们的href
属性中添加一个GET参数。
我想在适当的地方使用?
或&
。
例如,http://example.com
应变为http://example.com?extra=true
而http://example.com?already_got_a_param=true
应变为http://example.com?already_got_a_param=true&extra=true
。
什么代码会这样做?
答案 0 :(得分:4)
你可以用这个......
$('a').attr('href', function(index, href) {
return href + (this.search ? '&' : '?') + 'extra=true';
})