【问题标题】:return false/web development javascript/jquery [duplicate]返回 false/web 开发 javascript/jquery [重复]
【发布时间】:2014-09-16 14:35:35
【问题描述】:

我有一些这样的代码示例

---------------------- 第一个例子------

var rightFunction=function() {
        if (options.currentPage < options.numberOfPages) {
            options.currentPage += 1;
            ajaxConnection();
        }
        return false;
    };

    $('#newsContainer #rightSlider').click(rightFunction);
    $('#rightSliderHigh').click(rightFunction);

---------------------- 第二个例子-------------------

 $('#rightSliderPagination').click(function(){
        if (parseInt(_pagina_last_link.html()) >= options.numberOfPages) {
            return false;
        }
        _first_page_num = parseInt(_pagina_first_link.html());
        paginationGenerator(_first_page_num + options.centerNumber - 1);
        $('#slider-inner').slider("option", "value", _first_page_num + options.centerNumber - 1);
        return false;
    });

------------第三个例子 --------- -

$(".download").click(function(){
        if($(this).attr('goToPage')=='true') {
            var url=$(this).attr('href');
            document.location.href=url;
            return false;
        }

        var _this = $(this);
        var thistext = $("#bg-download-box").html();
        var _whattoshow = $(this).parent().find(".what-to-show").html();

        if (_whattoshow){
            $("#bg-download-box form").html(_whattoshow);
        }

        else{
            $("#bg-download-box").html("<div id='bg-download-box-inner' style='height:132px'></div>");
        }


        if ($(".reccomend-box").length != 0){
        $("#rightColumn").css("z-index",99999);
        }

        $("#bg-download-box").show();
        var whattocopy = $("#bg-download-box");
        $(whattocopy).insertAfter(_this);
        $("ul#songs-list").css("z-index","99999");


        $("ul#songs-list li.b-a-r").removeClass("emph");

        $(this).parent().parent().parent().addClass("emph");
        return false;
    });

为什么在这些函数的末尾使用 return false。拜托,有人可以解释一下 return false 的目的。

【问题讨论】:

标签: javascript jquery


【解决方案1】:

在示例 2 和 3 中,第一个返回 false 是为了阻止函数中任何进一步的代码的执行。

在示例 1 中,它实际上并没有做任何事情。虽然有时我自己会这样做,只是出于习惯和为未来阅读我代码的程序员的清晰性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多