jquery replace part of a url
I know practically nothing abou开发者_如何学Got jquery so please help me
I try this
function googlemapLinks {
$('div.gmnoprint a').attr("href").replace('ll','q');
}
it does not work
I have a <div class="gmnoprint">
In that div Google maps puts a javascript link in
maps.google.com/maps?ll=5.....
I need maps.google.com/maps?q=5.....
Can you show me a function I can drop in a script file?
try this:
function googlemapLinks {
var lnk = $('div.gmnoprint a').attr("href");
$('div.gmnoprint a').attr("href",lnk.replace('ll','q'));
}
For setting a value with attr
, you need to pass the new value as a second parameter. try this:
$('div.gmnoprint a').attr('href', $('div.gmnoprint a').attr('href').replace('ll','q'));
Bit late to the conversation but you could try:
$('.gmnoprint a').each(function() {
this.href = this.href.replace('ll','q');
})
The other functions seem to loop whereas this will find the link and then replace '11' with 'q'. Well, it should =P
Try this to change any link or a tag href inside the gmnoprint container. If you don't use the each loop all the hrefs will contain the first instance of the href on the page. this code looks at each a tag and replaces just that instance if it runs into 11 and changes the text to q
$('.gmnoprint a').each(function(){
$(this).attr('href', $(this).attr('href').replace('11','q'));
});
精彩评论