我正试图设计一个网站的人谁想要既联系我们的网页在底部部分的菜单和上面的标志在右上角。
因此,以下是客户端代码:
这是徽标上方的顶部菜单:

<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>

这是商标下面的菜单:
   <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 () {

但意识到这可能不起作用,因为它是索引。尽管我不确定。我试图将它们设置为“如果等价”,因此如果两个ref都相同,那么它将突出显示它们。我所做的一切都没有起作用(尽管有趣的是,我得到了一些突出其他导航的奇怪结果)。
那么,有什么建议吗?告诉我正确的方向?我没看到的东西,或者怎么能做到?这需要用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
    });

要突出显示页面匹配的元素,请添加以下内容(在上面的块之外)-
var currentPage = location.pathname.substring(1);
$('a[href="' + currentPage + '"]').closest('li').addClass('highlight'); // adds highlight to current page element

在fiddle中,我用jsfiddle的信息替换了位置信息,这样两个Contact Us元素都被突出显示-http://jsfiddle.net/jayblanchard/AEY5h/1/

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

10-13 01:24