访问href链接时更改div样式

时间:2015-06-06 00:24:56

标签: html css

我目前有一个包含这样div的html文件:



div.myClass:visited {
  background-color: red;
 }

<div class="myClass" href="link">
</div>
&#13;
&#13;
&#13;

我在这里要做的是将div直接指向给定的链接,如果已经访问了链接,则div background-color会变为红色。链接工作正常,但访问链接时更改背景颜色不起作用。有没有办法更改包含访问链接的div的背景颜色?谢谢!

2 个答案:

答案 0 :(得分:0)

使用Javascript替换innerHTML

<script type = "text/javascript">
    function ReplaceContentInContainer(id, content) {
    var container = document.getElementById(id);
    container.innerHTML = content;
} </script>

<div id="example1div" style="border-style:solid;padding:10px; text-align:center;">
I will be replaced when you click.
</div>
<a href="javascript:ReplaceContentInContainer('example1div','Whew! You clicked!')">Click me to replace the content in the container.</a>

答案 1 :(得分:0)

使用<a/>代替div,并将div设为display:block;widthheight

例如:

<a class="myClass" href="http://www.lego.com/en-us/"></a>

CSS:

 .myClass {
     background-color: blue;
     width:200px;
     height:200px;
     display:block;
 }

.myClass:visited {
     background-color: red;
 }

.myClass:hover{
     background-color: green;
}

View on CodePin

另见:

How can I detect visited and unvisited links on a page?

Privacy and the :visited selector