2009-11-24 30 views
1

我知道幾乎一無所知的jQuery,請幫我jQuery的更換URL的一部分

我試試這個

function googlemapLinks { 
$('div.gmnoprint a').attr("href").replace('ll','q'); 
} 

它不工作 我有一個<div class="gmnoprint">在該專區谷歌地圖把一個javascript鏈接 maps.google.com/maps?ll=5 ..... 我需要maps.google.com/maps?q=5 .....

你能告訴我一個函數嗎我可以放在腳本文件中?

回答

1

要設置值爲attr,您需要將新值作爲第二個參數傳遞。試試這個:

$('div.gmnoprint a').attr('href', $('div.gmnoprint a').attr('href').replace('ll','q')); 
3

試試這個:

function googlemapLinks { 
    var lnk = $('div.gmnoprint a').attr("href"); 
    $('div.gmnoprint a').attr("href",lnk.replace('ll','q')); 
} 
+0

謝謝d和TheVillageIdiot取代剛纔那個實例。看起來非常可行,但它不工作...也許是因爲它googlemap是由JavaScript寫入,而不是HTML? 是否有可能有可能搶在它LL該網址,並將它下面的文本,然後帶Q 功能appendMap(){取代 \t如果($(「塊gmap_location」)。長度> 0){ ('。block-gmap_location')。append($('

請參閱Google maps

')); \t} – kritter 2009-11-24 11:02:37

+1

如果它不工作,這可能是因爲代碼執行時$('div.gmnoprint a')'爲空。確保在顯示地圖後調用googlemapLinks(),並確保它存在。嘗試在瀏覽器的地址欄中輸入'javascript:alert($('div.gmnoprint a')。length);'以確保選擇器正在工作並返回所有項目 – 2009-11-24 13:43:56

0

有點晚的談話,但你可以嘗試:

$('.gmnoprint a').each(function() { 
    this.href = this.href.replace('ll','q'); 
}) 

其他功能似乎循環,而這將找到的鏈接,並然後用'q'代替'11'。那麼,它應該= P

0

試試這個更改gmnoprint容器內的任何鏈接或標籤href。如果你不使用每個循環,所有的hrefs將包含頁面上的第一個href實例。 此代碼看起來在每一個標籤,如果它運行到11並更改文本到q

$('.gmnoprint a').each(function(){ 
    $(this).attr('href', $(this).attr('href').replace('11','q')); 
});