javascript - 如何在 jQuery 中为 "active"滚动菜单的链接创建异常(exception)?

标签 javascript jquery html menu href

我已经得到了这段代码,它遍历了我所有的内部href,但我看不出能够弄清楚如何为链接到的链接之一创建异常div #section1.我需要某种围绕 section.addClass("active"); 行的 if 语句,但我不知道使用哪些条件来实现此目的。你能帮忙吗?

代码:http://jsfiddle.net/vD3vP/

    if (scrolledTo > target - threshold && scrolledTo < target + threshold) {

        //remove all selected elements
        sections.removeClass("active");

        //add current selected element.
        section.addClass("active");
    }

最佳答案

像这样:

$('a[href^="#"]:not(#exception)').click(function (event) {
    //do stuff
});

fiddle :http://jsfiddle.net/KyleMuir/vD3vP/1/

编辑:您可以像这样确定是否是异常:

//Smooth scroll when user click link that starts with #
$('a[href^="#"]').click(function (event) {

    if (!$(this).is('#exception')) {
        alert('Hi');
    }

    //prevent the browser from jumping down to section.
    event.preventDefault();

});

fiddle :http://jsfiddle.net/KyleMuir/vD3vP/2/

关于javascript - 如何在 jQuery 中为 "active"滚动菜单的链接创建异常(exception)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20035216/

相关文章:

JavaScript 数组

jquery - 如何使用复选框插件从 jstree 获取已检查的 id 列表?

html - 如何让标签在表 "cell"(td) 中右对齐?

javascript - 为什么当我滚动底部时无限滚动不起作用,而是在滚动顶部时触发?

javascript - 向特定设备发送通知

javascript - orderBy 按特定属性的 Angular 顺序

javascript - 使用should.js进行 Mocha 测试时出现问题

javascript - JavaScript 中三元条件运算符和逻辑与运算符的运算符优先级

jquery - 检测jQuery不支持的浏览器(二)

javascript - 为什么 FireFox 不能正确显示我的文本 (Arial)