2012-08-17 38 views
1

如何使用片段刷新當前頁面?javascript刷新帶片段的當前頁面

如果頁面網址類似:localhost/test#1<a href="http://localhost/test#2">如何刷新當前頁面?

location.reload(true) 

window.location.reload() 

這2種方法都將刷新頁面仍然爲localhost/test#1。謝謝。

回答

1

window.location.hash

setTimeout((
    function(){ 
     window.location.hash = '#2'; 
    } 
), 2000); 

// so: <a href="http://localhost/test#2" onclick="window.location.hash='#2';window.location.reload();">aaa</a> 
0

網址上的客戶端才起作用。部分後#不發送到服務器

0

您可以使用URL重寫

在你的.htaccess文件中添加這些行:

RewriteEngine On 
RewriteRule ^([0-9a-zA-Z_]*)$ index.php?p=$1 [L] 

在你的頁面的測試/ index.php文件,你可以使用鏈接(無#) :

<a href="2">Page 2</a> 

將其存儲在$ _GET [ 'p'](在示例中,目標是php)

找到滿足您需求的重寫規則。