簡體   English   中英

當由錨點URL訪問時,Hightlight錨點ID

[英]Hightlight Anchor ID when visited by an Anchor URL

編輯:

有人提到這在CSS中是可能的,所以我查了一下,這正是我所需要的!

的CSS

您需要使用:target偽類:

 :target { background-color: #ffa; } 

JS Fiddle演示。

感謝David Thomas的資料


還感謝您的代碼也是我所需要的


我已經嘗試了一段時間。

當有人通過以下網址訪問我的頁面時:

http://jsfiddle.net/maNR5/show/#second

我希望第二個id的標題元素以背景色突出顯示。

<div>

    <h1><a id="first">How can I...</a></h1>    

    <h1><a id="second">...make this...</a></h1>

    <h1><a id="turd">Highlighted when....</a></h1>    

    <h1><a id="furd">Visited by an...</a></h1> 

    <h1><a id="earth">Anchor URL</a></h1>


</div>

這可能在javascript中嗎? 感謝您的提示。

嘗試使用:

var hash = window.location.hash;
$('#'+hash).parent().css('background-color','red');

嘗試這個:

 var id=window.location.href.substring(window.location.href.lastIndexOf('/') + 1);
 $(id).css('background-color','red');

嘗試這個。 每當您單擊任何鏈接時,其顏色都會更改。 表示已訪問它。

$("h1").each(function(a,b){
    $(this).find('a').click(function(){
        console.log(this);
        $(this).css('color','#333399');
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM