简体   繁体   中英

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 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM