Hightlight Anchor ID when visited by an Anchor URL

一个人想着一个人 提交于 2019-12-11 08:43:33

问题


EDIT:

Someone mentioned this was possible in CSS so I looked it up and that is exactly what I needed!

CSS

You need to use the :target pseudo-class:

:target {
   background-color: #ffa;
}

JS Fiddle demo.

Thanks David Thomas source


ALSO THANKS MILIND YOUR CODE IS ALSO WHAT I NEEDED


I've been trying to figure this out for a while.

When someone visits my page trough an URL like this:

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

I want the header element with id second to be highlighted with a background color.

<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>

Is this possible in javascript? Thanks for any tips.


回答1:


Try this:

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



回答2:


Try to use:

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



回答3:


try this. when ever you click on any of the links change its color. Indicates that it is visited.

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


来源:https://stackoverflow.com/questions/23009299/hightlight-anchor-id-when-visited-by-an-anchor-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!