CSS- Highlight a div when the id is linked to usin

2019-01-23 00:51发布

What I am attempting to do is to highlight a div with a certain id, when It has been referred to by an anchor on another page IE:

User clicks link href="qw.html#test", when the page is loaded, then the div with the id="test" is highlighted so that the user can see it clearly.

I'm sure that I've seen a CSS3 example where a div is highlighted if it was linked to. Or was it JavaScript?

3条回答
在下西门庆
2楼-- · 2019-01-23 01:17

You need to use the :target pseudo-class:

:target {
   background-color: #ffa;
}

JS Fiddle demo.

查看更多
虎瘦雄心在
3楼-- · 2019-01-23 01:18

Javascript can be used to dynamically add/change the class of the div:

If you have:

<div id="test"></div>

Javascript function, executed by the click of the anchor:

document.getElementById("test").className += " highlighted";

Result:

<div id="test" class=" highlighted"></div>
查看更多
Anthone
4楼-- · 2019-01-23 01:29

You can do this in JavaScript. Refer to How to get the anchor from the URL using jQuery? on how to get the anchor from URL and then it can be something simple like

document.getElementById(hash).style.backgroundColor="Yellow";
查看更多
登录 后发表回答