开发者

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜