在jQuery事件之后停止onClick返回页面顶部

时间:2011-03-28 14:31:50

标签: jquery onclick return

我在这里阅读了很多关于这个问题的帖子,并提供了很好的解决方案,但是,在我的案例中似乎都没有。

这是我的JS:

$(document).ready(function(){
    $("a.tab").click(function () {
        $(".active").removeClass("active");
        $(this).addClass("active");
        $(".inside ul").fadeOut();
        $(".inside ul").fadeIn();
        var content_show = $(this).attr("title");
        return false;
        e.preventDefault();
    });
    $("a.tab, h4").FontEffect({
        outline: true
    })
});

我做错了什么?

更新:

感谢您的帮助。现在我有以下内容,但它仍然无效:

$(document).ready(function(){
        $("a.tab").click(function(e) {
            e.preventDefault();
            $(".active").removeClass("active");
            $(this).addClass("active");
            $(".inside ul").fadeOut();
            $(".inside ul").fadeIn();
            var content_show = $(this).attr("title");
            return false;
        });
        //$("a.tab, h4").FontEffect({
        //  outline: true
    //  })
    });

主播标签:

<a href="#" title="content_1" class="tab active">New Videos</a>

VIEWABLE

3 个答案:

答案 0 :(得分:1)

问题是你在阻止默认操作(e.preventDefault();)之前返回,试试这个:

    $("a.tab").click(function (e) {
        e.preventDefault();
        $(".active").removeClass("active");
        $(this).addClass("active");
        $(".inside ul").fadeOut();
        $(".inside ul").fadeIn();
        var content_show = $(this).attr("title");
        return false;
    });

并在锚标签上确保href的格式为:

href='javascript:void(0);'

答案 1 :(得分:1)

尝试:

 $("a.tab").click(function (e) {

在代码中,您遗漏了event argument variable,在这种情况下,e将是<{1}}。

如果您使用的是preventDefault()方法,则无需使用return false。

在线演示: http://jsfiddle.net/dcFT7/

更新

滚动跳转不是由链接上的点击引起的,这是因为当ul隐藏$(".inside ul").fadeOut()时页面高度变小,因此浏览器滚动到顶部。

我可以使用.animate({ opacity: 0 }, 500)来修复fadeOut效果,而不会隐藏它。

$("a.tab").click(function (e) {
    ...
    $(".inside ul").animate({opacity:0},500);
    $(".inside ul").fadeIn();
    ...
    e.preventDefault();
});

答案 2 :(得分:0)

可能就像“e.preventDefault();”一样简单在错误的路线上?

$(document).ready(function(){
        $("a.tab").click(function () {
            e.preventDefault();
            $(".active").removeClass("active");
            $(this).addClass("active");
            $(".inside ul").fadeOut();
            $(".inside ul").fadeIn();
            var content_show = $(this).attr("title");
            return false;

        });
        $("a.tab, h4").FontEffect({
            outline: true
        })
    });