javascript - 用了return false,超链接为什么还是跳转了?

查看:96
本文介绍了javascript - 用了return false,超链接为什么还是跳转了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

<style type="text/css">
        *{ margin:0; padding:0;}
        body {font-size:12px;text-align:center;}
        a { color:#04D; text-decoration:none;}
        a:hover { color:#F50; text-decoration:underline;}
        .SubCategoryBox {width:600px; margin:0 auto; text-align:center;margin-top:40px;}
        .SubCategoryBox ul { list-style:none;}
        .SubCategoryBox ul li { display:block; float:left; width:200px; line-height:20px;}
        .showmore { clear:both; text-align:center;padding-top:10px;}
        .showmore a { display:block; width:120px; margin:0 auto; line-height:24px; border:1px solid #AAA;}
        .showmore a span { padding-left:15px; background:url(img/down.gif) no-repeat 0 0;}
        .promoted a { color:#F50;}


    </style>
    
</head>
<body>
    <div class="SubCategoryBox">
        <ul>
            <li><a href="#">佳能</a><i>(30440) </i></li>
            <li><a href="#">索尼</a><i>(27220) </i></li>
            <li><a href="#">三星</a><i>(20808) </i></li>
            <li><a href="#">尼康</a><i>(17821) </i></li>
            <li><a href="#">松下</a><i>(12289) </i></li>
            <li><a href="#">卡西欧</a><i>(8242) </i></li>
            <li><a href="#">富士</a><i>(14894) </i></li>
            <li><a href="#">柯达</a><i>(3091) </i></li>
            <li><a href="#">宾得</a><i>(9520) </i></li>
            <li><a href="#">理光</a><i>(2195) </i></li>
            <li><a href="#">奥林巴斯</a><i>(4114) </i></li>
            <li><a href="#">明基</a><i>(12205) </i></li>
            <li><a href="#">爱国者</a><i>(1466) </i></li>
            <li><a href="#">其他</a><i>(7275) </i></li>
        </ul>
    
        <div class="showmore">
            <a href="more.html"><span>显示全部品牌</span></a>
        </div>
    </div>
    <script type="text/javascript" src = "../css/jquery-3.1.0.js"></script>
    <script type="text/javascript">
        
$(document).ready(function(){
    var $category = $("ul li:gt(5):not(:last)");
    $category.hide();
    var $toggleBtn = $("div.showmore > a")    ;
    $toggleBtn.click(function() {
        if($category.is(":visible")){
            $category.hide();
            $(".showmore a span")
                .css("background","url(img/down.gif no-repeat 0 0")
                .text("显示全部品牌");
            $("ul li").removeClass("promoted");
        }else{
            $category.show();
            $(".show more a span")
                .css("background","url(img/up.gif no-repeat 0 0")
                .text("精简显示品牌");
            $("ul li")
                .fiter(":contains('佳能'),:contains('尼康'),:contains('奥林巴斯')")
                .addClass("promoted")    
        }
        return false;

    });
})
    </script>
</body>
</html>

解决方案

你的代码问题有2:

1.

.text("精简显示品牌");

这个分号是中文的

2.

.fiter(":contains('佳能'),:contains('尼康'),:contains('奥林巴斯')")

fiter拼写错误, 正确应该是filter

由于这两个错误, 导致js报错, 所以后面的js不执行了, return false;自然就没效果了.
使用return false, 是可以阻止默认事件, 这没问题的..

return false
等同于下面两句
event.preventDefault()
event.stopPropagation()

这篇关于javascript - 用了return false,超链接为什么还是跳转了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆