2013-05-31 26 views
1

我有一個隱藏的<div>的一堆頁。我希望能夠直接鏈接到它們中的任何一個,並讓它們顯示標籤是否與id匹配。我已經有如何顯示一個隱藏的div如果哈希匹配它的ID

var thisHash = window.location.hash; 
if(window.location.hash) { 
    $(thisHash).show(); 
} 

我需要確保每其他<div>雖然隱藏。如果散列匹配<div>,我可以添加一個類,但是我不知道如何去檢查<div>idid是否與.hash匹配。

回答

3

使用CSS它:

div { display: none; } 
div:target { display: block; } 

如果你真的想要的JavaScript:

$('div').hide().filter(location.hash).show(; 
+0

CSS強烈可取的,因爲瀏覽器將隱藏的div只要在佈局中看到它,而js腳本可能會在body.onload上運行。 – Tommi

+0

非常感謝Tommi,但頁面頂部也有選項卡,所以我想我可能不得不使用JavaScript。 –

+0

您可以輕鬆地限制選擇器,因此並非每個div都是針對性的,但僅以某個類爲例。 – ThiefMaster

0

這應該做的伎倆:

if (window.location.hash) { 
    $(window.location.hash).show(); 
}