简体   繁体   中英

Replacing a href element with jQuery

I have to change part of the link.

Exactly I have to change "/?" to "/all?" in every a href on the site.

I was reading something about that, but nothing works.

Can you help me?

Greetings.

Try:

$('a').each(function () {
    $(this).attr('href', $(this).attr('href').replace('/?', '/all?'))
});

jsFiddle example

This solution you wont need jQuery, i'm also assuming you desire to change every match of /? in your URL, if not, use only replace('/?','/all?')

JSFiddle -> http://jsfiddle.net/eZgmD/

var anchors = document.getElementsByTagName('A');
for (var i = 0; i<anchors.length; i++)
{
      anchors[i].href = anchors[i].href.replace(/\/\?/g,'/all?') 
};
$('a').attr('href', function(){
    return this.href.replace('/?', '/all?');
});

.attr can be passed a function as the assigning argument.

To change the href on a link you use this Jquery function:

$("a").attr("href", "http://www.google.com/")

Where the web site is changed to what you want to overwrite on the href attribute.

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