1

我有 4 个li元素,我希望在悬停 ( mouseenter, mouseleave) 时每个人都为选择的每个人更改背景颜色。

我用这个代码做这个工作:

<script>
        $( document ).ready(function() 
        {
            $('#center-group').children().on('mouseenter',function(){
                    $(this).children('.topmenu').children('.r-part').css('background-color','#810000');
                    $(this).children('.topmenu').children('.r-part').children('.r-part-bot').css('background-color','#980000');
            });
            $('#center-group').children().on('mouseleave',function(){
                    $(this).children('.topmenu').children('.r-part').css('background-color','#404040');
                    $(this).children('.topmenu').children('.r-part').children('.r-part-bot').css('background-color','#4c4c4c');
            });
        });
        </script>

现在我想在选择(click event)时 li 的每个人都改变背景颜色并且不工作顶级事件!怎么办???请指导我....

4

1 回答 1

0

这个正确的代码:

$(document).on('mouseenter','#center-group li',function(){
                    $(this).children('.topmenu').children('.r-part').css('background-color','#810000').children('.r-part-bot').css('background-color','#980000');;
            });
            $(document).on('mouseleave','#center-group li',function(){
                    $('#center-group').children().not('.selected').children('.topmenu').children('.r-part').css('background-color','#404040').children('.r-part-bot').css('background-color','#4c4c4c');
            });

            $(document).on('click','#center-group li',function(){
                $(this).toggleClass('selected');
                $(this).siblings().removeClass('selected');
                $('#center-group').children('.selected').children('.topmenu').children('.r-part').css('background-color','#810000').children('.r-part-bot').css('background-color','#980000');
                $('#center-group').children().not('.selected').children('.topmenu').children('.r-part').css('background-color','#404040').children('.r-part-bot').css('background-color','#4c4c4c');
于 2013-07-04T08:57:03.863 回答