保持CSS下拉菜单项用php突出显示
在上面的链接上,您可以看到我有一个 CSS 下拉菜单在我的网站上。它工作正常,但是,我希望顶级项目在我位于它们所代表的页面上时保持突出显示。
到目前为止我已经有了,但它不起作用(我只显示一个菜单项,它没有任何子菜单,因为它节省空间)
这是 HTML:
<ul>
<li><h2><a name="donate" id="donate" href="index2.php?op=Donate">Donate</a></h2></li>
</ul>
这是为背景着色的 CSS:
#menu a {
color: #000;
background: #efefef;
text-decoration: none;
}
每页的内容由$head的值决定:$head = $_GET['op'];
我尝试通过将其直接放在菜单后面来实现更改:
if($head == "Donate") {
echo '<style>
#menu a donate {
color: #000;
background: #fff;
text-decoration: none;
</style>';
}
当我将“捐赠”从上面的代码中删除时:“#menu a {”所有菜单项的背景颜色都会更改为白色,但我需要更改特别是“捐赠”按钮。我尝试通过将 id="donate" / name="donate" 添加到菜单项(如上所示),然后在 css 中使用“#menu a donate {”来调用它。但这显然是错误的,因为它不起作用!我应该怎么办?
On the link above you can see that I have a CSS drop down menu in my site. It works fine, however, I want the top level items to stay highlighted when I'm on the page they represent.
I have this so far but it won't work (I'm only showing one menu item which doesn't have any sub menus as it saves space)
Here's the HTML:
<ul>
<li><h2><a name="donate" id="donate" href="index2.php?op=Donate">Donate</a></h2></li>
</ul>
Here's the CSS that colours the background:
#menu a {
color: #000;
background: #efefef;
text-decoration: none;
}
The content of each page is determined by the value of $head: $head = $_GET['op'];
I tried to implement the change by placing this straight after the menu:
if($head == "Donate") {
echo '<style>
#menu a donate {
color: #000;
background: #fff;
text-decoration: none;
</style>';
}
When I leave 'donate' out of the above code: '#menu a {' the background color of all the menu items changes to white, but I need to change the 'donate' button specifically. I tried doing this by adding id="donate" / name="donate" to the menu item (as seen above), and then calling it in css with '#menu a donate {'. but that is obviously wrong as it doesn't work! What should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为你需要使用
作为选择器,因为 donate 是 a 标签的 id,因此它紧跟在 a 标签之后(没有空格),前面有一个“#”。
您还缺少 css 声明的“}”右括号。
I think you need to use
as a selector instead, because donate is an id for the a tag, therefore it comes immediately after the a tag (no spaces) with a "#" in front.
You were also missing a "}" closing bracket for the css declaration.
好的,快速而肮脏的答案:你的 CSS 选择器将无法工作,因为现在它会在 id 为“menu”的标签内的“a”标签内搜索标签“donate”。我假设您的所有链接都有一个特定的 Id,因此最简单的方法是使用此选择器
作为一个额外的好处,此选择器将可以更快地被浏览器解析。
顺便说一句,你似乎没有关闭样式标签。这是一个错误吗?
现在,对于更长的答案,这并不是最好的方法。我建议您创建一个名为“currentpage”的 CSS 类,并在菜单中像这样使用它
,这样您就可以将样式保留在样式表中,这样更容易维护。当然,如果您所有的菜单标签都是手工编码的,您可能会发现在每个标签中添加条件非常繁琐。如果确实如此,我建议您使用循环创建菜单。
顺便说一句,您应该删除 a 标记中的 name 属性,它是一个已弃用的功能。 id 的工作做得很好。
OK, quick and dirty answer : your CSS selector wont work, because right now it search for a tag "donate" inside a "a" tag inside a tag with the id "menu". I assume all your link have a specific Id, so the easy way to do it is to use this selector
As an added bonus, this selector will be faster to parse by the browser.
By the way, you seem not to close the style tag. Is that an error?
Now, for a longer answer, it is not exactly the best way to do it. I suggest you create a CSS class with a name like "currentpage" and to use it like this in your menu
That way you can keep your style in the stylesheet where it will be easier to maintain. Now of course, if all your menu tags are handcoded, you may find it pretty tedious to add the condition in everytag. If it's indeed the case, I suggest you create your menu using a loop.
By the way, you should remove the name attribute in the a tag, its a deprecated feature. id does the job just fine.
在这种情况下,您通常会为当前突出显示的项目使用 CSS 类:
这样,您就不会更改 id、名称或其他任何内容 - 只是:
如有必要,可以从 Javascript 添加该类,但也可以使用如下所示的方法从 PHP 生成该类:
In this kind of situations, you will generally use a CSS class for the currently highlighted item :
This way, you won't change the id nor name nor anything else -- but just :
That class can be added from Javascript, if necessary -- but it can also be generated from PHP, using something like this :
usgin css 你可以这样尝试:
使用 jquery 可以按如下方式完成:
首先创建一个如下所示的 css:
然后编写 jquery 代码:
usgin css you can try like this:
using jquery this can be done as following:
First create a css like below:
then write jquery code:
您应该使用 php 为当前页面项设置一个类,并使用 css 为该类设置规则。
You should set a class for the current page item using php, and use css to set rules for that class.