Jquery switch 语句偶尔有效

发布于 2024-12-20 13:41:43 字数 1721 浏览 3 评论 0原文

我对编写自己的 jquery 函数还很陌生,我发现调试它非常困难,因为将错误消息放入谷歌时并没有太大帮助。

我有一个页面锚点的导航菜单,当单击每个锚点时,屏幕滚动到锚点,元素将根据哪一个锚点改变颜色,悬停颜色也会改变。我认为真的很简单。

滚动始终有效,动画偶尔有效,悬停有效通常我必须单击链接两次。 return false 仅适用于您单击的第一个链接。

这使用了scrollTo和animate-colors插件。

谁能告诉我我做错了什么?

$(".scrolltoanchor").click(function() {
                                $('a').removeClass('selected');
                                $(this).addClass('selected');
        $.scrollTo($($(this).attr("href")), {
            duration: 750
        });

        switch ($(this).attr("id")) {
            case 'personal':
            $('.scrolltoanchor').animate({color: '#E4D8B8'});
            $(".scrolltoanchor").hover(
            function() {
            $(this).css('color', 'blue');
            },function(){
            $(this).css('color', '#E4D8B8');
            });
            break;

            case 'achievements':
            $('.scrolltoanchor').animate({color: '#ffffff'});
            $(".scrolltoanchor").hover(
            function() {
            $(this).css('color', 'red');
            },function(){
            $(this).css('color', '#ffffff');
            });
            break;

            case 'skills':
            $('.scrolltoanchor').animate({color: '#dddddd'});
            $(".scrolltoanchor").hover(
            function() {
            $(this).css('color', 'orange');
            },function(){
            $(this).css('color', '#ffffff');
            });
            break;


        }

        return false;
    });

很抱歉要求被灌输,但我遵循了我所学到的我认为正确的语法。有什么我应该知道的事情会阻止它按我的预期工作吗?

编辑:抱歉,我忘记了,我在(平均)每秒单击滚动到锚点链接时收到此错误

未捕获的类型错误:无法读取未定义的属性“0”

我无法发现真正的模式。有时它似乎只发生在以前没有被点击过的那些上,有时则不会。 谢谢

I am quite new to writing my own jquery functions and I find debugging it very difficult as the error messages aren't too helpful when put into google.

I have a navigation menu for page anchors that when each one is clicked the screen scrolls to the anchor, the elements will change color depending on which one and the hover color will also change. Very simple really, I think.

The scrolling always works, the animate works occasionally and the hover works put usually I have to click the link twice. The return false only works on the first link you click.

This uses the scrollTo and animate-colors plugins.

Can anyone tell me what I'm doing wrong?

$(".scrolltoanchor").click(function() {
                                $('a').removeClass('selected');
                                $(this).addClass('selected');
        $.scrollTo($($(this).attr("href")), {
            duration: 750
        });

        switch ($(this).attr("id")) {
            case 'personal':
            $('.scrolltoanchor').animate({color: '#E4D8B8'});
            $(".scrolltoanchor").hover(
            function() {
            $(this).css('color', 'blue');
            },function(){
            $(this).css('color', '#E4D8B8');
            });
            break;

            case 'achievements':
            $('.scrolltoanchor').animate({color: '#ffffff'});
            $(".scrolltoanchor").hover(
            function() {
            $(this).css('color', 'red');
            },function(){
            $(this).css('color', '#ffffff');
            });
            break;

            case 'skills':
            $('.scrolltoanchor').animate({color: '#dddddd'});
            $(".scrolltoanchor").hover(
            function() {
            $(this).css('color', 'orange');
            },function(){
            $(this).css('color', '#ffffff');
            });
            break;


        }

        return false;
    });

Sorry to ask to be spoonfed, but I have followed what I believed to be the correct syntax from what I have learnt. Is there something I should know that is stopping this working as I expect?

EDIT: Sorry I forgot, I get this error on the (on average) every second click of a scrolltoanchor link

Uncaught TypeError: Cannot read property '0' of undefined

I can't spot a real pattern. Sometimes it seems to happen only on ones that havent been clicked before, sometimes not.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

握住你手 2024-12-27 13:41:43

你采取了错误的方法。

您应该绑定悬停处理程序一次,并根据单击的处理程序决定颜色。

最简单的方法可能是将颜色数据存储在查找表中,其中键是元素的 ID。

var ids = {
    personal: {
        over:'blue',
        out:'#E4D8B8'
    },
    achievements: {
        over:'red',
        out:'#ffffff'
    },
    skills: {
        over:'orange',
        out:'#dddddd'
    }
};
var current = ids.personal;

然后绑定处理程序一次,并使用单击的处理程序的 id 来设置当前颜色集。

var scroll_anchors = $(".scrolltoanchor");

scroll_anchors.hover( function() {
    $(this).css( 'color', current.over );
},function(){
    $(this).css( 'color', current.out );
});

scroll_anchors.click(function() {
     $('a.selected').removeClass('selected');
     $(this).addClass('selected');

     $.scrollTo($($(this).attr("href")), { duration: 750 });

     current = ids[ this.id ]; // set the current color set based on the ID

     scroll_anchors.animate({ color: current.out });

     return false;
});

You're taking the wrong approach.

You should bind the hover handlers once, and decide the colors based on which one was clicked.

Simplest way would probably to store the color data in a lookup table where the keys are the IDs of the elements.

var ids = {
    personal: {
        over:'blue',
        out:'#E4D8B8'
    },
    achievements: {
        over:'red',
        out:'#ffffff'
    },
    skills: {
        over:'orange',
        out:'#dddddd'
    }
};
var current = ids.personal;

Then bind the handlers once, and use the id of the one clicked to set the current color set.

var scroll_anchors = $(".scrolltoanchor");

scroll_anchors.hover( function() {
    $(this).css( 'color', current.over );
},function(){
    $(this).css( 'color', current.out );
});

scroll_anchors.click(function() {
     $('a.selected').removeClass('selected');
     $(this).addClass('selected');

     $.scrollTo($($(this).attr("href")), { duration: 750 });

     current = ids[ this.id ]; // set the current color set based on the ID

     scroll_anchors.animate({ color: current.out });

     return false;
});
这样的小城市 2024-12-27 13:41:43

当您多次调用 .hover() 时,您并没有删除旧的事件处理程序,而只是添加了一个新的事件处理程序。每次都会调用每个处理程序。您需要首先调用 .unbind("hover")

$(".scrolltoanchor").unbind("hover").hover(function () {
    ...
});

您还可以绑定到 switch 语句之外的悬停,以消除一些代码重复:

$(".scrolltoanchor").click(function () {
    $('a').removeClass('selected');
    $(this).addClass('selected');
    $.scrollTo($(this.href), {
        duration: 750
    });
   var off, on;
    switch (this.id) {
        case 'personal':
            off = '#E4D8B8';
            on = 'blue';
            break;
        case 'achievements':
            off = '#ffffff';
            on = 'red';
            break;
        case 'skills':
            off = '#dddddd';
            on = 'orange';
            break;
    }

    $('.scrolltoanchor')
        .animate({ color: off })
        .unbind("hover")
        .hover(function () {
            $(this).css('color', on);
        }, function () {     
            $(this).css('color', off);
        });

    return false;
});

When you call .hover() multiple times, you aren't removing the old event handlers, you are just adding a new one. Each handler will be called each time. You'll want to call .unbind("hover") first:

$(".scrolltoanchor").unbind("hover").hover(function () {
    ...
});

You can also bind to hover outside of the switch statement to eliminate some of the code duplication:

$(".scrolltoanchor").click(function () {
    $('a').removeClass('selected');
    $(this).addClass('selected');
    $.scrollTo($(this.href), {
        duration: 750
    });
   var off, on;
    switch (this.id) {
        case 'personal':
            off = '#E4D8B8';
            on = 'blue';
            break;
        case 'achievements':
            off = '#ffffff';
            on = 'red';
            break;
        case 'skills':
            off = '#dddddd';
            on = 'orange';
            break;
    }

    $('.scrolltoanchor')
        .animate({ color: off })
        .unbind("hover")
        .hover(function () {
            $(this).css('color', on);
        }, function () {     
            $(this).css('color', off);
        });

    return false;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文