2013-02-08 46 views

回答

2

像這樣的東西應該工作

$('a').each(function() { 
    $(this).prop('href',$(this).prop('href') + '&z=3'); 
}) 

循環每個a元素,並添加z=3href財產結束

1

你可以這樣做:

$("a").attr('href', function(c, b) { 
    return b + "&z=3"; 
}); 
+0

+1比我的努力簡單很多..不錯 – ManseUK

0

這應該做的伎倆。

$('a').each(function(){ 
    this = $this; 
    curr_url = this.attr('href'); 
    this.attr('href', 'curr_url' + '&z=3'); 
}); 
1
$('a[href^="http://www.example.com"]').prop('href', function(i, href){ 
    return href + '&z=3'; 
}) 
+0

+1不錯,很贊 – ManseUK

0

你需要檢查this.attr( 'href' 屬性)。的indexOf( '?')> -1之前只是增加一個鍵/值對。如果沒有'?'你也需要添加。

您可能最好在javascript中使用本地href API來解析URL,然後將需要的位添加到需要修改的URL部分。

http://james.padolsey.com/javascript/parsing-urls-with-the-dom/

你或許應該還會考慮做這個用「的」事件觸發(jQuery的最新)。如果添加到onLoad鉤子中,增加DOM的速度很慢,並且會延遲頁面顯示。

$('body').on('click','a',function(e){ 
    // Prevent it jumping straight out 
    e.preventDefault; 
    // Define vars 
    var href = this.href, 
     qstr = 'a=b&bar=foo'; 
    // check for structure of url and add query string 
    this.href += ((href.indexOf('?') > -1) ? '&': '?') + qstr; 
    // jump 
    return true; 
}); 
相關問題