简体   繁体   English

单击链接时如何保持活动状态

[英]How can I keep active state when clicking link

I know an active element is when you click the link. 我知道一个活动元素是当您单击链接时。 Is there anyway to carry the active state across to the next page. 无论如何,有没有将活动状态传递到下一页的信息。

Is there anyway to do it via javascript so it removes an active state and adds the active state to the next link. 无论如何,可以通过javascript进行操作,因此它会删除活动状态并将该活动状态添加到下一个链接。 So i'm having a bit of problems with the active state, if anyone can help me out. 因此,如果有人可以帮助我,我的活动状态就会有些问题。

Here is the HTML 这是HTML

<ul class="list-type">
<li class="active"><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
<li><a href="#">Item 6</a></li>
</ul>

CSS 的CSS

ul.list-type {
list-style-type: none;
padding: 0;
margin-left: 0;
text-align: left;
font-size: 16px;

}
ul.list-type li {
padding: 10px 25px 10px 25px;
}
ul.list-type li a {
color: #000;
}
ul.list-type li a:active {
color: #F00;
}
ul.list-type li a:hover {
color: #CCC;
}

Here is a JS Fiddle: 这是一个JS小提琴:

http://jsfiddle.net/dDcXC/ http://jsfiddle.net/dDcXC/

You can get the URL from document.URL from there it's as simple as iterating all links and comparing their href You can try something like: 您可以从document.URL获取URL,从那里获取URL就像迭代所有链接并比较它们的href一样简单。您可以尝试执行以下操作:

var links = document.getElementsByTagName('a');
for (var i= 0 ; i<links.length ; i++){
 if(links[i].href == document.URL){
    var currentClass = links[i].className;
    links[i].className = currentClass + " active"; 
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM