c# - 突出显示重复的 <ul></ul> 菜单导航

标签 c# javascript jquery html css

我正在尝试为那些既想要菜单底部的“联系我们”页面又想要右上角 Logo 上方的人设计一个网站。

这里是客户端代码:

这是 Logo 上方的顶部菜单:

<ul class="topnavigation" style="width:1000px; border-bottom-style: none;  height: 40px;">
        <li class="highlight" style="width:100px; height: 40px; font-family:Calibri; float:right;"><a href="ContactUs.aspx">Contact Us</a></li>
        <li style="width:100px; height:40px; font-family:Calibri; border-left:1px solid white; border-right:1px solid white; float:right;"><a href="StartPage.aspx">Home</a></li>
    </ul>

这是 Logo 下的菜单:

   <ul class="navigation" style="width:1000px; height:40px; border-bottom:none;">
    <li style="width:150px; font-family:Calibri; height: 40px; border-right:1px solid white;"><a href="AboutUs.aspx">About Us</a></li>
    <li style="width:150px; font-family:Calibri; border-right:1px solid white; height: 40px;"><a href="Application.aspx">Applications</a></li>
    <li style="width:200px; font-family:Calibri; border-right:1px solid white; height: 40px;"><a href="FeaturesAndBenefits.aspx">Features and Benefits</a></li>
    <li style="width:200px; font-family:Calibri; border-right:1px solid white; height: 40px;"><a href="TechnicalSpecs.aspx">Technical Specs</a></li>
    <li style="width:150px; font-family:Calibri; border-right:1px solid white; height: 40px;"><a href="ContactUs.aspx">Contact</a></li>
    <li style="width:145px; font-family:Calibri; border-right:none; height: 40px;"><a href="Recognition.aspx">Recognition</a></li>
        </ul>

为了突出显示用户选择的页面,我使用了一些 javascript(我最近一直在努力学习)和 CSS

JavaScript:

$(document).ready(function () {
var str = location.href.toLowerCase();

$('.topnavigation li a').each(function () {
    if (str.indexOf(this.href.toLowerCase()) > -1) {
        $("li.highlight").removeClass("highlight");
        $(this).parent().addClass("highlight");
    }
});

$('.navigation li a').each(function () {
    if (str.indexOf(this.href.toLowerCase()) > -1) {
        $("li.highlight").removeClass("highlight");
        $(this).parent().addClass("highlight");
    }
});
});

CSS:

ul.navigation
{
margin: 0px;
padding: 0px;
list-style: none;
background-color:#0071BB;
height:34px;
border-bottom:none;
}

ul.navigation li
{ 

float: left;
position: relative;
top: 0px;
left: 0px;

}
ul.navigation li a:last-child{}


ul.navigation a
{
color:white;
display: block;
padding: 8px 8px;
text-decoration: none;
 }
 /*background color of LI*/
 ul.navigation li.highlight
 {
 background:Darkblue;
 }
 /*Text color for A*/
 ul.navigation li.highlight a
 { 
color:white;
  }
 ul.navigation li:hover
{
color:white;
background-color:darkblue;
background: darkblue;
 }

a, a:visited
{
color:#000;
 }



ul.topnavigation
{
margin: 0px;
padding: 0px;
list-style: none;
background-color:#0071BB;
height:34px;
border-bottom:none;
}

ul.topnavigation li
{ 

float: left;
position: relative;
top: 0px;
left: 0px;

}
ul.topnavigation li a:last-child{}


ul.topnavigation a
{
color:white;
display: block;
padding: 8px 8px;
text-decoration: none;
}
/*background color of LI*/
ul.topnavigation li.highlight
{
 background:Darkblue;
}
/*Text color for A*/
ul.topnavigation li.highlight a
{ 
color:white;
}
ul.topnavigation li:hover
{
color:white;
background-color:darkblue;
background: darkblue;
}

使用此实现,如果用户单击任何页面,它会突出显示该页面。但是,如果他们单击右上角的“联系我们”,则只会突出显示底部菜单中的“联系我们”,而不是顶部菜单。我发现这很奇怪并且对我来说本身就是一个问题,因为我希望它突出显示顶部而不是底部。 (如果有人也能回答这个问题,我将不胜感激 - 因为我不明白它是如何识别它的)。

那么,我怎样才能同时突出显示顶部联系页面导航和底部联系页面导航。我假设这将使用 java 脚本而不是 C# 代码完成。

我试图将两者结合起来,例如

  $('.navigation li a' & '.topnavigation li a').each(function () {

但意识到这可能行不通,因为它正在编制索引。虽然我不确定。我试图将它们设置为“如果相等”,所以如果两个 href 相同,那么它会突出显示它们。我所做的一切都没有奏效(尽管有趣的是我得到了一些突出显示其他导航的奇怪结果)。

那么,有什么建议吗?给我指出正确的方向?我没有看到的东西或如何做到这一点?这是否需要在 C# 中完成? JavaScript 可以做到吗?

请告诉我。这是我提出的第一个问题,所以我对此感到很沮丧。

最佳答案

你真的不需要这里的每个,也不需要组合选择器,除非你要根据它们的根类做一些特殊的事情。你只需要一种方法来匹配事物。这是一个演示 - http://jsfiddle.net/jayblanchard/AEY5h/

编辑:原始代码仍然有效(您需要为您的站点删除 e.preventDefault();)

 $('li a').click(function(e) {
        e.preventDefault(); // just for this demo
        var thisHREF = $(this).attr('href'); // get the href of the clicked item
        $('li').removeClass('highlight'); // remove all the classes
        $('a[href="' + thisHREF + '"]').closest('li').addClass('highlight'); // add the class to the items that have the same href
    });

要突出显示您的页面匹配的元素,请添加以下内容(在上述 block 之外)-

var currentPage = location.pathname.substring(1);
$('a[href="' + currentPage + '"]').closest('li').addClass('highlight'); // adds highlight to current page element

在 fiddle 中,我用 jsfiddle 的信息替换了位置信息,以便突出显示两个“联系我们”元素 - http://jsfiddle.net/jayblanchard/AEY5h/1/

关于c# - 突出显示重复的 <ul></ul> 菜单导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23633097/

相关文章:

javascript - nodejs导入需要转换

javascript - 如何在 3 延迟后删除元素?

javascript - Bootstrap 悬停图像显示示例

c# - 为什么 C# 不支持变体泛型类?

javascript - asp.net 无法加载 css3 派

c# - asp,net 使用 site.master 页面

javascript - 我如何在 Javascript 中旋转图像?

jquery - 使用 jQuery 计算 div 的高度 - 减去页眉和页脚

c# - 带有 Toastr 的 ASP.Net MVC C# 不缩小 javascript

c# - 参数序列化——指定命名空间