我想知道这两个函数之间的区别是什么?

时间:2011-07-09 22:15:47

标签: jquery

功能1

// Toggle the sharing section
    // Start with hiding the icon list
    $("#shareThisPost").click(function(){
        $("#shareThisPost").fadeOut("slow");

        $(function() {
            $("div#shareSection ul").fadeIn("slow");
            $shareState = 1;
        });
    });

功能2

// Toggle the sharing section
        // Start with hiding the icon list
        $("#shareThisPost").click(function(){
            $("#shareThisPost").fadeOut("slow", function() {
                $("div#shareSection ul").fadeIn("slow");
                $shareState = 1;
            });
        });

2 个答案:

答案 0 :(得分:4)

第二个将在淡出结束后开火。

首先只是两个单独的函数首先点击事件触发然后淡出开始然后淡入开始。第二个点击事件触发。然后淡出淡出,在淡出结束后淡入开始。

正如你所说,“,”是fadeOut函数的参数。如果您有一个功能定义

,请考虑这种方式
function x(param1, param2) {
    //Do something
}

褪色的结构就像这样。

function fadeOut(param1, param2) {
     // This time param2 is a call back function and param one is the speed fade.
}

在js中,函数可以像变量一样传递,因此param2作为参数传递给淡出函数,并在fadeOut操作完成后在fadeOut内部调用。

在另一种情况下,fadeOut启动然后调用下一个动作。因此,如果您真的想看看发生了什么,请尝试使用1000的淡出时间(param1)运行这两个函数。这真的会让您有时间看看两者之间的区别。

所以要回答你的问题,排序与“,”之间的区别,而没有的是在一个函数作为参数传递给淡出而另一个函数运行,因为它是自己的实体。

答案 1 :(得分:2)

$("#shareThisPost").click(function(){
            $("#shareThisPost").fadeOut("slow", function() {
                $("div#shareSection ul").fadeIn("slow");
                $shareState = 1;
            });
        });

以下代码在fadeOut发生后执行,这称为回调,

$("div#shareSection ul").fadeIn("slow");
                $shareState = 1;
            });

这对document.ready来说是相当的,我不明白为什么它在点击功能中

$(function() {
            $("div#shareSection ul").fadeIn("slow");
            $shareState = 1;
        });
相关问题