2013-03-01 67 views
0

我想在一個按鈕的點擊更新菜單項的HREF更新查詢字符串,這裏是我的代碼創建或無插件

function refreshReportMenu(){ 
    var ts = '&ts=' + new Date().getTime().string(16); 
    $('ul.nav a.reps').each(function(){ 
     var href = $(this).attr('href'); 
     $(this).attr('href', href + ts); 
    }); 
} 

但這裏的問題是,它總是追加TS到HREF,我想更新ts值,如果它已經在href中。

+0

你們是不是要更新瀏覽器的查詢字符串,或者只是更換任何查詢字符串中的鏈接href屬性? – adeneo 2013-03-01 11:43:59

+0

只在href屬性 – coure2011 2013-03-02 19:10:19

回答

0

以及..你也許可以嘗試

var reg = /(^|&)ts=(.*?)(&|$)/, ts = 'ts=' + new Date().getTime().toString(16); 
$('ul.nav a.reps').each(function(){ 
    var href = $(this).attr('href'); 
    if (href.match(reg)) { 
     href = href.replace(reg,"$1+"+ts+"$3"); 
    } else { 
     href += "&" + ts; 
    } 
    $(this).attr('href', href); 
}); 

它應該工作:)

+0

上述代碼中的幾個問題,除了它的完美的更新代碼與.getTime()。toString(16)和href.match( reg) – coure2011 2013-03-04 09:03:07

+0

哈哈哈,是的,在這裏輸入adhoc可以是... P – 2013-05-10 18:26:30

0
$('ul.nav a.reps').each(function(){ 
     var href = $(this).attr('href'); 
     $(this).attr('href', href + ts); 
    }); 

想你去錯在這裏$this而不是$(this)

+0

thx爲修復,但我正在尋找一種解決方案來更新查詢字符串 – coure2011 2013-03-01 11:42:15

0

嘗試.indexOf

alert('Index: ' + $(this).attr('href').indexOf('ts')); 

檢查

if($(this).attr('href').indexOf('ts') > 0) 
{ 
    $(this).attr('href', href + ts); 
}