1

そのため、URL に含まれる ID に基づいて div を非表示にするスクリプトを作成しています。これは私がこれまでに持っているものです:

<script>
$(function(){ 
if (document.location.href.indexOf('#aberdeen') > 0) 
$("#centralia, #chewelah, #everett, #harbour-pointe").hide();

if (document.location.href.indexOf('#centralia') > 0) 
$("#aberdeen, #chewelah, #everett, #harbour-pointe").hide();

if (document.location.href.indexOf('#chewelah') > 0) 
$("#aberdeen, #centralia, #everett, #harbour-pointe").hide();

if (document.location.href.indexOf('#everett') > 0) 
$("#aberdeen, #chewelah, #centralia, #harbour-pointe").hide();

if (document.location.href.indexOf('#harbour-pointe') > 0) 
$("#aberdeen, #chewelah, #everett, #centralia").hide();

if (document.location.href.indexOf('#') < 0)
    $(".directory").show();
});
</script>

ただし、URL に ID が追加されていても、それらのすべてが表示されます。何か案は?

4

4 に答える 4

7

これは「ID」ではなく「ハッシュ」と呼ばれます。document.location.hashではなく にありますdocument.location.href

if (document.location.hash === '#aberdeen') {
    $("#centralia, #chewelah, #everett, #harbour-pointe").hide();
}
于 2012-07-17T19:14:16.530 に答える
2

これを試してください。アイテムにクラスを追加して使用できますdocument.location.hash

$(function(){ 
   var id = document.location.hash;
   $('.all').not(id).hide() // hide them expect the element that has an id of hash
});
于 2012-07-17T19:14:20.590 に答える
0

上記のJavascriptソリューションの代わりに、CSSでこれを行うことができます(最新のブラウザーのみ)。

たとえばhashdisplayクラスなど、すべてのdivを指定する場合、次のスタイルを使用できます。

.hashdisplay { display: none; }
.hashdisplay:target { display: block; }

http://jsfiddle.net/9qCYU/

警告!!:target疑似クラスをサポートするブラウザでのみ機能します!

http://reference.sitepoint.com/css/pseudoclass-target

于 2012-07-17T19:25:29.243 に答える
0

試す :

if (document.location.hash.indexOf('#aberdeen') != -1) {
    $("#centralia, #chewelah, #everett, #harbour-pointe").hide();
}
于 2012-07-17T19:13:53.810 に答える