2010-03-02 55 views
3

我希望啓用JavaScript的用戶查看頁面results.php。 JavaScript禁用的用戶應該看到results_d.php。更改href文件,但沒有與jQuery查詢字符串

要做到這一點,我最初顯示與results_d.php的鏈接。 然後,通過使用JavaScript更改鏈接中的目的地,只有啓用了JavaScript的用戶才能看到豐富的內容。

在這裏有幾個關於使用jQuery來更改整個href目的地的問題,但是如何才能更改文件名並保持查詢字符串的原樣?

我在想這樣的事情,但它不工作...

$(document).ready(function() 
{ 
    $('a').attr('href').replace('results_d', 'results'); 
}); 

回答

2
$(document).ready(function() { 
    $('a').attr('href', function(index, href) { 
     return 'results?'+(href.split('?')[1]) 
    }); 
}); 
+0

href is undefined – user284502 2010-03-04 10:57:16

+0

適合我......問題可能在於,選擇器有點泛泛。也許它會幫助,如果你想要一個類的鏈接,你想改變,然後調用'$(「a.your_class」)。attr(...)' – harpax 2010-03-04 11:51:56

0
$(document).ready(function() { 
    $('a').each(function() { 
    $(this).attr('href',$(this).attr('href').replace('results_d', 'results')); 
    }); 
}); 
+0

感謝您的答覆,但他們似乎並不爲我工作。 這裏是我的鏈接之一... [代碼] View all results [/代碼] – user284502 2010-03-04 09:38:38

+0

$(本).attr( 「HREF」)是未定義 – user284502 2010-03-04 10:58:04

0

感謝您的回答傢伙。儘管在我的文件中,但我得到了一些邏輯錯誤。我已將錯誤添加到評論中。

到底,什麼對我所做的工作是:

$('a').each(function() 
{ 
    this.href = this.href.replace('results_d', 'results'); 
}); 
0

jQuery(function(){ 
 
    
 
    jQuery("a").each(function(e,i){ 
 
    
 
    jQuery(this).attr("href",i.href.replace("results_d","results")); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<a href="/search/results_d.php?kwds=whatever&amp;search_mode=abc&amp;countries[]=AL‌​L" class="medtxt">View all results</a> 
 
<a href="/search/results_d.php?asd=whatever&amp;search_mode=abc&amp;countries[]=AL‌​L" class="medtxt">View all results</a> 
 
<a href="/search/results_d.php?kwdfgds=whatever&amp;search_mode=abc&amp;countries[]=AL‌​L" class="medtxt">View all results</a>

我知道這是有點晚了,但我不是計算器在2010年和我不能拒絕回答這個沒有答案的問題。

謝謝!

+0

噢!我剛剛看到你已經在javascript中給出了答案。請忽略! – Arcanyx 2015-04-27 06:24:51