使用jquery更改menue的背景颜色

时间:2022-11-21 16:23:37

I am trying to build a photo gallery with Zenphoto. They use php and one can add a custom menue like this:

我正在尝试用Zenphoto建立一个照片库。他们使用PHP,可以添加这样的自定义菜单:

    <div id="navmenu">
    <?php printCustomMenu('main_menue'); ?>
    </div>

I changed the appearance of the whole thing in the sylesheet, which looks like this:

我改变了sylesheet中整个事物的外观,看起来像这样:

#navmenu {
        width: 1000px;
        height: 42px;
        margin: 0px auto 30px auto;
        font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
        text-align: left;
        font-size: 21px;
        background-color: #000000
        }

#navmenu li {
        display: inline; 
        }

#navmenu a {
        color: #eee;
        display: inline;
        line-height: 2em;
        padding: 0.375em 0.5em;
        text-decoration: none;
            }

#navmenu a:hover {
        font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
        font-size: 21px;
        color: #000000;
        background-color: #ffff33;
        padding: 0.375em 0.5em;
            }

Now I want to change the background-color of the individual menu items, so that every menu item has its own color. Random or not I don't care. I created a js file that is wired correctly.

现在我想更改单个菜单项的背景颜色,以便每个菜单项都有自己的颜色。随机与否我不在乎。我创建了一个正确连接的js文件。

I have tried several bits of code I found, but nothing works. Now I tried to do this to see if I can change the color at all:

我已经尝试了几个代码,但没有任何效果。现在我试着这样做,看看我是否可以改变颜色:

 $(document).ready(function() {

 $("navmenu").hover(function(){
  $(this).css('background-color', '#eeeeee')
 });
});

Does not work. I am new to all this programming and I would greatly appreciate any help. It would be super nice if you could answer for dummies, so that I can understand.

不起作用。我是所有这些编程的新手,我将非常感谢任何帮助。如果你能回答傻瓜,那将是非常好的,这样我才能理解。

3 个解决方案

#1


4  

Use:

使用:

$("#navmenu").hover(function(){

You missed the ID # selector.

你错过了ID#选择器。

#2


0  

You need to properly address the div using # for an ID or a . for a class:

您需要使用#为ID或a正确地处理div。对于一个类:

$(document).ready(function() {

$("#navmenu").hover(function(){
  $(this).css('background-color', '#eeeeee')
 });
});

A tip for beginners: if you're not getting the result you expect, you can verify that the function is being called by throwing in a console log message anywhere like this:

初学者的提示:如果你没有得到你期望的结果,你可以通过在任何地方抛出控制台日志消息来验证函数是否被调用:

$(document).ready(function() {
    console.log("document ready!");
    $("#navmenu").hover(function(){
        console.log("hover activated");
        $(this).css('background-color', '#eeeeee')
    });
});

#3


0  

You could give something like this a try as it will pick a random color on hover and switch back to the #EEE background on the hover out event:

你可以尝试这样的东西,因为它会在悬停时选择一个随机颜色并切换回hover out事件的#EEE背景:

jQuery:

jQuery的:

$(function () {
    $("#navmenu a").hover(function () {
        var newColor = Math.floor(Math.random() * 16777215).toString(16);
        $(this).css('background-color', '#' + newColor);
    }, function () {
        $(this).css('background-color', '#EEE')        
    });
});​

Working Example: http://jsfiddle.net/Qc4R7/

工作示例:http://jsfiddle.net/Qc4R7/

#1


4  

Use:

使用:

$("#navmenu").hover(function(){

You missed the ID # selector.

你错过了ID#选择器。

#2


0  

You need to properly address the div using # for an ID or a . for a class:

您需要使用#为ID或a正确地处理div。对于一个类:

$(document).ready(function() {

$("#navmenu").hover(function(){
  $(this).css('background-color', '#eeeeee')
 });
});

A tip for beginners: if you're not getting the result you expect, you can verify that the function is being called by throwing in a console log message anywhere like this:

初学者的提示:如果你没有得到你期望的结果,你可以通过在任何地方抛出控制台日志消息来验证函数是否被调用:

$(document).ready(function() {
    console.log("document ready!");
    $("#navmenu").hover(function(){
        console.log("hover activated");
        $(this).css('background-color', '#eeeeee')
    });
});

#3


0  

You could give something like this a try as it will pick a random color on hover and switch back to the #EEE background on the hover out event:

你可以尝试这样的东西,因为它会在悬停时选择一个随机颜色并切换回hover out事件的#EEE背景:

jQuery:

jQuery的:

$(function () {
    $("#navmenu a").hover(function () {
        var newColor = Math.floor(Math.random() * 16777215).toString(16);
        $(this).css('background-color', '#' + newColor);
    }, function () {
        $(this).css('background-color', '#EEE')        
    });
});​

Working Example: http://jsfiddle.net/Qc4R7/

工作示例:http://jsfiddle.net/Qc4R7/