开发者

How to combine these two javascript/jquery scripts

I have an id #navigation li a and a class .menu-description . I want to change text color of the class .menu-description when hovered on #navigation li a

My jquery so far:

<script>
$(document).ready(function() {
  $('#navigation li a').mouseover(function() {

  //Check if element with class exists, if so change it to new
           if ($('div.menu-description').length != 0)
                $('div.menu-description').removeClass('menu-description').addClass('menu-descriptionnew');
           //Check if element with cla开发者_如何学JAVAss 'menu-descriptionnew' exists, if so change it to 'menu-description'
           else if ($('div.menu-descriptionnew').length != 0)
                $('div.menu-descriptionnew').removeClass('menu-descriptionnew').addClass('menu-description');
                });
});
</script>

second script:

<script>
$(document).ready(function() {
    $('.menu-description').hover( function(){

        $(this).css('color', '#EEEEEE');

    },
    function(){

        $(this).css('color', '#000000');

    });

});
</script>

How to combine these to in order to reach for my wanted result?


<script>
$(document).ready(function() {
  $('#navigation li a').mouseover(function() {

  //Check if element with class exists, if so change it to new
           if ($('div.menu-description').length != 0)
                colorA();
           //Check if element with class 'menu-descriptionnew' exists, if so change it to 'menu-description'
           else if ($('div.menu-descriptionnew').length != 0)
                $colorB();
                });
});

function colorA()
{
   $(this).css('color', '#EEEEEE');
}

function colorB(){
   $(this).css('color', '#000000');
}

$('.menu-description').hover(colorA,colorB);
);


Why not just:

$(document).ready(function() {
  $('#navigation li a').mouseover(function() {

  //Check if element with class exists, if so change it to new
           if ($('div.menu-description').length != 0)
                $('div.menu-description').removeClass('menu-description').addClass('menu-descriptionnew');
           //Check if element with class 'menu-descriptionnew' exists, if so change it to 'menu-description'
           else if ($('div.menu-descriptionnew').length != 0)
                $('div.menu-descriptionnew').removeClass('menu-descriptionnew').addClass('menu-description');
                });

    $('.menu-description').hover( function(){

        $(this).css('color', '#EEEEEE');

    },
    function(){

        $(this).css('color', '#000000');

    });

});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜