How can I add a GET param to some anchor elements in jQuery?
I have a bunch of a
elements and I want to add a GET param to their开发者_Go百科 href
attribute.
I want to use ?
or &
where appropriate.
For example, http://example.com
should become http://example.com?extra=true
and http://example.com?already_got_a_param=true
should become http://example.com?already_got_a_param=true&extra=true
.
What code would do that?
You could use this...
$('a').attr('href', function(index, href) {
return href + (this.search ? '&' : '?') + 'extra=true';
})
jsFiddle.
精彩评论