开发者

How can I add a GET param to some anchor elements in jQuery?

开发者 https://www.devze.com 2023-03-20 18:51 出处:网络
I have a bunch of a elements and I want to add a GET param to their开发者_Go百科 href attribute.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消