2013-03-01 22 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); 
}