[英]How to change class named active into link's active attribute in javascript
我正在使用superfish作为菜单。因为它没有active class
来表示当前页面选项卡,所以我添加了以下javascript。
<script type="text/javascript">
var path = window.location.pathname.split('/');
path = path[path.length-1];
if (path !== undefined) {
$("ul.sf-menu")
.find("a[href$='" + path + "']") // gets all links that match the href
.parents('li') // gets all list items that are ancestors of the link
.children('a') // walks down one level from all selected li's
.addClass('active');
}
</script>
我还根据脚本的要求在CSS中添加了一个名为active的类。
.sf-menu a.active{
margin-top:-5px;
height: 51px;
padding-top: 15px;
z-index: 100;
background-color:#072438;
}
它工作得很好,但是由于某些原因我想从
a.active into a:active
但是,如何在javascript中更改此部分以适合CSS?
.addClass('active');
您不能添加一个pseudo-class
中,你不能添加同样的方式使用JavaScript的元素pseudo-class
直列式style=''
属性。
您可以做的是使用javascript更改样式表,以添加所需的规则:
document.styleSheets[0].insertRule('a:active { color: red; }'
,0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.