- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 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/
我正在尝试使用 jQuery 来提交一个基本的 Accordion 样式菜单。 这是菜单: http://www.cybart.com/bscg/ 这是为其提供 Accordion 功能的代码片段:
这是我的网站: http://www.simply-a-christian.com/index.html 在导航栏上,如果您移动到: 信念 玛丽安教义 您可以看到每个 Marian Dogma(列出了
这可能是一个很容易回答的问题,但是,我找不到解决方案。我已经建立了一个非常简单的菜单,每当我将鼠标悬停在 ul 父项上时,它的子项 ul ul 就会变得可见。基本上它是有效的,但 parent 让位于
这是一张图片: 问题是(你可以看到):“页面”按钮太低,无法点击。我想要的是它应该靠近“联系人”。 现在是代码: HTML Home About
我有一个这样的嵌套 ul 列表: Subcat Subcat2
如果标题不好理解,我来解释一下... 我有一个 css/jquery 切换菜单。按“+”号时它变大,按“-”号时它变小。我使几个元素的填充、边距和高度在切换/单击时发生变化。菜单顶部充满了链接,然后当
我正在尝试创建一个列表,该列表会在单击上/下时更改它的第一个元素。 This is what i want to do. 此代码运行良好。但问题是我也在我网站的主菜单中使用列表,并且这段代码也在我的主
我需要使用水平缩放的 ul > li > ul 的组合来创建一个菜单。每个 ul 都应该有一个 max-height 并在溢出时滚动。悬停在 li 上时,如果里面有另一个 ul 标签,它应该会在右侧打
我正在 ul 中编写 ul 来制作 Accordion 式菜单。但是当我在 html 验证器中检查下面的代码时,它给了我这些错误 Element ul not allowed as child of
我有两个列表,第二个列表是可排序的,我可以从第一个列表拖动到第二个列表,但不知道如何从第一个列表中删除。 如果我删除 helper: "clone" ,该项目 (li) 会被删除,但拖放操作效果不佳(
JSFiddle 问题:https://jsfiddle.net/td6szj3o/ /我有一个 ul li,每个 li 都有标准文本项。该列表是动态的,因此有时每个内容 li 小于其所在容器的 wi
我有两个元素 - 一个在另一个里面。问题是第二个元素显示在第一个 ul li 元素的顶部。 HTML: Home Merchants
我有一些结构如下的菜单: First level link 1 Child link 1 Child link 2
我有很长的ul,李的数目不是恒定的 1 2 3 4 5 6 7 8 9 我想使用Moovweb SDK将其分为两个ul。 1 2 3 4
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎偏离主题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或 include a mini
我有一棵信息“树”,显示在许多嵌套列表中(ul li ul li ....)。两个问题: 当我点击时,父 ul 和子 ul 都会切换。 如果我点击在其中没有 ul 的嵌套列表中(例如,下面的 Cran
我正在尝试在 jQuery 中创建以下内容: 我有一个项目列表(例如 10 个)。如果项目数超过 6,则将剩余项目(从 6 到 10)复制到新的 UL 中以创建下拉菜单结构。 菜单的第一个版本是 ht
嘿大家 - 我在工作中尝试 jquery 的第二天,有点卡住了。 我有一个无序列表的链接..在无序列表的链接中 点击 ,我想要的 child 变得可见。 点击(文档上的任何位置)后,我想要 child
嗨,这就是我想要实现的目标。我有一个 ID="columns"的 div,在本例中我有 3 列,但可能会有更多或更少...我正在使用 JQuery 动态添加列表项。
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Using .after() to add html closing and open tags 我想显示高度几乎相
我是一名优秀的程序员,十分优秀!