访问菜单选项未突出显示

时间:2015-09-22 16:13:25

标签: javascript jquery html css web

所以我一直在看这个jquery插件,它突出显示当前页面上当前的菜单项。我目前希望菜单项边框和文本改变颜色。我试图实现这一目标的CSS方法是。选择我可能在这里真的很傻,但是我似乎无法让它工作。任何帮助将非常感激。我的JS被分成一个单独的文件夹。

<script type="text/javascript" src="jquery-1.11.3.min.js"></script>
<script src="js/pageMenu.js" type="text/javascript" ></script>

<div class="container">
    <header class="site-header">
        <img id="logo" src="logo.png" alt="logo">
           <nav id="nav"<a href="#"></a>
       <ul id="site-nav">
        <li><a href="index.php">Home</a>
        </li>
         <li><a href="#">Used Cars</a>
         </li>
         <li><a href="aboutus.php">About us</a>
         </li>
         <li><a href="#">Contact Us</a>
       </li>
    </ul>
</nav>

这是JS

$(document).ready(function(){
$('#site-nav a').each(function(index) {
    if(this.href.trim() == window.location)
        $(this).addClass("selected");
  });
});

最后CSS(我知道这是一团糟)

#site-nav {
margin: 0px;
padding: 0px;
line-height: 1;
}

#site-nav li {
float: left;
list-style: none;
}

#site-nav li a:link, #site-nav li a:visited {
font-family: 'Pontano Sans', sans-serif;
font-size:14px;
font-weight: bold;
display: block;
padding: 12px 17px;
text-decoration: none;
border-radius: 0px 0px 8px 8px;
/* horizontal radius / vertical radius */
list-style: none;
border-style: outset;
border-width: 1px;
background: #FFFFFF;
background: rgba(226, 226, 226, 1);
background: -moz-linear-gradient(top, rgba(226, 226, 226, 1) 0%, rgba(219,      219, 219, 1) 3%, rgba(209, 209, 209, 1) 51%, rgba(254, 254, 254, 1) 100%);
background: -webkit-gradient(left top, left bottom, color-stop(0%, rgba(226, 226, 226, 1)), color-stop(3%, rgba(219, 219, 219, 1)), color-stop(51%, rgba(209, 209, 209, 1)), color-stop(100%, rgba(254, 254, 254, 1)));
background: -webkit-linear-gradient(top, rgba(226, 226, 226, 1) 0%, rgba(219, 219, 219, 1) 3%, rgba(209, 209, 209, 1) 51%, rgba(254, 254, 254, 1) 100%);
background: -o-linear-gradient(top, rgba(226, 226, 226, 1) 0%, rgba(219, 219, 219, 1) 3%, rgba(209, 209, 209, 1) 51%, rgba(254, 254, 254, 1) 100%);
background: -ms-linear-gradient(top, rgba(226, 226, 226, 1) 0%, rgba(219, 219, 219, 1) 3%, rgba(209, 209, 209, 1) 51%, rgba(254, 254, 254, 1) 100%);
background: linear-gradient(to bottom, rgba(226, 226, 226, 1) 0%, rgba(219, 219, 219, 1) 3%, rgba(209, 209, 209, 1) 51%, rgba(254, 254, 254, 1) 100%);
filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#e2e2e2', endColorstr='#fefefe', GradientType=0);
}

 #site-nav a:focus, #site-nav a:hover {
color: #DC2F35;
background-color: #FFFFFF;
}
.selected {
color: #DC2F35;
border-color: #DC2F35;
}

3 个答案:

答案 0 :(得分:0)

问题出在if块的情况下,因为JavaScript不允许你使用==运算符来比较字符串。您必须使用localeCompare()函数来比较字符串 例如: -
var str1 =&#34; cd&#34 ;;
var str2 =&#34; ab&#34 ;;
var n = str1.localeCompare(str2);


有关更多信息,请参阅this

答案 1 :(得分:0)

您可能应该在构建菜单的php中添加所选类。这会阻止您网页上的任何FOUC

我不知道这是不是很糟糕,但我会把你链接到这个答案: How to have the class="selected" depending on what the current page/url is

答案 2 :(得分:0)

正如所料,我实际上是愚蠢的。该代码有效,但我没有在我的其他文件中正确链接javascript