[英]JS selected item not showing properly in css menu
CSS 的CSS
#myMenu ul li {
display: inline;
}
#myMenu ul li a {
background-color:#333333;
text-transform: uppercase;
font-weight: bold;
font-family:"Open Sans", Arial, sans-serif;
font-size:12px;
text-decoration: none;
padding: 1em 2em;
color: #000000;
border-left:1px solid #333333;
border-right:1px solid #333333;
border-top:1px solid #333333;
}
#myMenu ul li a:hover {
color: #fff;
background-color: #999999;
}
.selection {
background-color: #000000;
border-bottom:10px solid #000000;
border-top:9px solid #000000;
}
HTML 的HTML
<div align="right" id="myMenu">
<ul>
<li><a href="#" data-id="appstructurediv">Structure</a></li>
<li id="style"><a href="#" data-id="appstylediv">Style</a></li>
<li><a href="#" data-id="appdetailsdiv">Details</a></li>
</ul>
JavaScript 的JavaScript
$(function () {
$("li:first-child").addClass("selection");
$('li').click(function () {
$('#myMenu li').removeClass('selection');
$(this).addClass('selection');
});
});
I want to add black colour to selected items background, but the above code is not working. 我想为选定的项目背景添加黑色,但是上面的代码不起作用。
If I remove background-color:#333333
from #myMenu ul li a
it works. 如果我从
#myMenu ul li a
删除background-color:#333333
,它将起作用。
You are adding the background color via CSS to the <a>
element. 您正在通过CSS将背景色添加到
<a>
元素。 You are targeting the <li>
element with your jQuery. 您正在使用jQuery定位
<li>
元素。 For all you know, the jQuery could be working properly, but you can't see it because the <a>
element has a background color. 就您所知,jQuery可以正常工作,但是您看不到它,因为
<a>
元素具有背景色。
Try changing the jQuery to target the <a>
element instead. 尝试将jQuery更改为以
<a>
元素为目标。 Be sure to return false
or use preventDefault()
when targeting the <a>
element. 定位
<a>
元素时,请确保return false
或使用preventDefault()
。
Also, as a side note, unless you have something else going on which I don't know about, it would be more efficient just hard code the selection class to the first element instead of targeting it with jQuery. 另外,作为一个旁注,除非您有其他我不知道的事情发生,否则将选择类硬编码到第一个元素而不是使用jQuery定位它会更有效。 Just a tip.
只是一个小费。
There are a couple of issues here. 这里有几个问题。
li
so the context will be the li
element li
绑定在一起,因此上下文将成为li
元素 #myMenu ul li a
initially #myMenu ul li a
Change your code and CSS to: 将您的代码和CSS更改为:
CSS: CSS:
#myMenu ul li a.selection {
background-color: #000000;
border-bottom:10px solid #000000;
border-top:9px solid #000000;
}
Code: 码:
$(function () {
$("li:first-child").addClass("selection");
// Or move your handler to the anchor rather than the list item
$('li').click(function () {
$('#myMenu li a').removeClass('selection');
$(this).find('a').addClass('selection');
});
});
Also to add to Chris's answer...the css for #myMenu ul li a
will always outweigh the .selection
because it is has more specificity. 也添加到克里斯的回答......对CSS
#myMenu ul li a
永远大于.selection
因为它有更多的特异性。 You will need to add !important
to any styles in .selection
if you want them to override the styles in #myMenu ul li a
您将需要添加
!important
在任何风格.selection
如果你希望它们覆盖在风格#myMenu ul li a
Generally although jQuery does auto selection automatically I personally like to handle it myself, just because then I know my code is "selecting" what I want. 通常,尽管jQuery会自动执行自动选择,但我个人还是想自己处理它,因为那时我知道我的代码正在“选择”我想要的东西。
So Like this: 所以像这样:
$(function () {
$("li:first-child").addClass("selection");
$('li').each(function(){ //loop all li elements
$(this).click(function () {
$('#myMenu li.selection').removeClass('selection'); //select the li
// with the selection class
$(this).addClass('selection');
});
});
});
But basically an issue I see with how you did it is there is no margin on you anchor, or padding on your li, witch could make the changes to the li invisible. 但是基本上我遇到的问题是您的操作方式是锚钉上没有边距,或者在li上没有填充,女巫可以使li的更改不可见。
Used with the following it should tell you if this works 与以下内容一起使用时,它应该告诉您是否可行
.selection{
background-color: #000000;
border-bottom:10px solid #000000;
border-top:9px solid #000000;
padding:5px; //change to suit your needs
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.