Jquery document ready函数参数的用途是什么?

Jquery document ready函数参数的用途是什么?,jquery,Jquery,在此代码中,函数C的含义是什么 处理者 类型:函数事件eventObject[,任何额外参数][,…]为: 触发事件时要执行的函数。值false也可以作为简单返回false的函数的简写 因此,functionc是事件发生时将执行的函数的头部;c是传递给事件体的事件对象。许多程序员使用event或e,因此更容易记住参数的含义: .on( events [, selector ] [, data ], handler ); 就其本身的功能而言,C毫无意义,也毫无用处;它是匿名函数事件处理程序的一部

在此代码中,函数C的含义是什么

处理者 类型:函数事件eventObject[,任何额外参数][,…]为:

触发事件时要执行的函数。值false也可以作为简单返回false的函数的简写

因此,functionc是事件发生时将执行的函数的头部;c是传递给事件体的事件对象。许多程序员使用event或e,因此更容易记住参数的含义:

.on( events [, selector ] [, data ], handler );
就其本身的功能而言,C毫无意义,也毫无用处;它是匿名函数事件处理程序的一部分,如果您愿意,它将在每次事件发生时执行。

$document.ready在参数中使用侦听器函数,以便在DOM准备好文档时执行

jQuery函数作为参数传递给ready listener函数

这在调用$.noConflict时非常有用,因为在这种情况下,$变量将被还原,不再引用jQuery

不依赖$通常是一个好的做法,因为您永远不知道$将被调用还是不调用

以下是更有意义的:

function(c) //head of handler
{ //body of handler start
    //the code that will be executed
} //body of handler end
jQuery(document).ready(function ($) {
    //can use $ safely to reference jQuery in here
});

(function ($) {
    //can use $ safely to reference jQuery in here
    $(document).ready(function () {

    });
})(jQuery);
现在,对于其他表单,例如$'.alert close'。在“单击”时,函数c{then c不指向jQuery。相反,它引用了可用于获取事件详细信息或取消/停止其传播的

请注意,如果您没有使用事件对象,则只需执行$'.alert close'。单击,函数{

最后,按照惯例,人们将对事件对象使用e变量,而不是c变量

以下是如何重新编写以使其更有意义:

function(c) //head of handler
{ //body of handler start
    //the code that will be executed
} //body of handler end
jQuery(document).ready(function ($) {
    //can use $ safely to reference jQuery in here
});

(function ($) {
    //can use $ safely to reference jQuery in here
    $(document).ready(function () {

    });
})(jQuery);
在你的例子中,c可以被命名为任何我更喜欢称之为event的东西。 它是在触发后发出的回调 回调包含事件对象jQuery 事件对象基于W3提供的标准输出 事件对象包含连接到触发器的数据。在on函数中,它包含以下数据:

typeArg参数是一个字符串。 canBubbleArg参数是一个布尔值。 cancelableArg参数是一个布尔值。 viewArg参数是实现AbstractView接口的对象。 detailArg参数是一个数字。 screenXArg参数是一个数字。 screenYArg参数是一个数字。 clientXArg参数是一个数字。 clientYArg参数是一个数字。 ctrlKeyArg参数是一个布尔值。 altKeyArg参数是一个布尔值。 shiftKeyArg参数是一个布尔值。 metaKeyArg参数是一个布尔值。 buttonArg参数是一个数字。 relatedTargetArg参数是实现EventTarget接口的对象。 看


不需要使用回调。但在某些情况下非常有用,例如在ajax调用中

C是事件侦听器对象。如果事件侦听器对象有任何属性,则可以在函数调用中调用它们。所有事件都不同,因此您必须查看API,以查看是否有任何需要的属性你的代码。就目前情况而言,c是不必要的,因为你没有使用它

下面是一个JSFIDLE示例:

以下是小提琴的javascript代码:

jQuery(document).ready(function($) {
  $('.alert-close').on('click', function() {
    $(this).parent().fadeOut('slow');
  });
});
以下是示例HTML:

      $(document).ready(function(c) {
      $('.alert-close').on('click', function(c){


$(this).parent().fadeOut('slow', function(c){

    /*
      c is the event listener object
      some peopele use e or event instead but it doesn't matter

      you can prevent default actions of elements
      like links or form submission buttons by using the event
      listener object.

      for example:
      would stop the default action of an element
      the div used doesn't have one so it will
      create an error and return undefinded
      c.preventDefault();

      another example:
      This would stop the event from going up the document object
      model to other elements
      It's not necessary here though because you are using the
      parent() function to select the parent div only
      so this will also return an error of undefined
      c.stopPropagation();
     */

      /*
      this will display the event listener object it will be  
      undefined on the jquery function you are using
      */
       //alert(c);

    /* 
       A good use for the event listener would be to capture
       capture keycodes of the keyboard presses display them or
       do perform an action if a user presses a certain key like
       up arrow for example
    */

      //you could do something like this
    $(window).on("keypress",function(event){

        //display the keycode pressed
        //which is the property of the event object in this
        //case
        alert(event.which);

    });

       });
       });
     });

@西蒙扎克先生感谢您的关注,请让我知道…它是做什么的?它会在文档准备好后执行。