2014-11-16 71 views
0
$(document).ready(function(c) { 
    $('.alert-close').on('click', function(c){ 
    $(this).parent().fadeOut('slow', function(c){ 
    }); 
    }); 
}); 

在这段代码中function(c)是什么意思?文档就绪函数参数的用途是什么?

+0

事件监听器。 – simonzack

+0

@simonzack先生感谢您的关注,请让我知道......它做了什么? – Mani

+0

它在文档准备就绪时执行。 – plalx

回答

0

jQuery.on() | jQuery API Documentation

.on(events [, selector ] [, data ], handler); 
  • 处理 Type: Function(Event eventObject [, Anything extraParameter ] [, ... ])是:

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

因此,function(c)是事件发生时将执行的函数的头部; c是传递给事件主体的事件对象。许多程序员使用evente所以要记住的说法表示会更容易:

function(c) //head of handler 
{ //body of handler start 
    //the code that will be executed 
} //body of handler end 

在它自己的function(c)意味着什么,并没有任何用处;它的一部分匿名功能,事件处理程序,如果你愿意的话,每次发生event时都会执行。

0

$(document).ready在文档(DOM)准备就绪时需要执行参数中的侦听器函数。

jQuery函数作为参数传递给ready侦听器函数。

$.noConflict()被调用时这很有用,因为在这种情况下,$变量被恢复并且不再引用jQuery

通常不会依赖$,因为您永远不会知道$.noConflict()将被调用或不被调用。

这里就是会更有意义:

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').on('click', function(c) {然后c没有指向jQuery。相反,它引用event object,其中可以使用获取有关该事件的详细信息或取消它/停止传播。

请注意,它不是使用事件对象,那么你可以做$('.alert-close').on('click', function() {

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

下面是如何这可能会被改写,使更多的意义:

jQuery(document).ready(function($) { 
    $('.alert-close').on('click', function() { 
    $(this).parent().fadeOut('slow'); 
    }); 
}); 
0
  • 你的情况c可以任意命名(我更喜欢称之为event)。
  • 它是一种trigger
  • 后给出的Callback containes Event Object(jQuery的Documentation
  • Event Object一个Callback这就是基于由W3给出的标准输出(ECMAScript

Event Object包含数据连接到触发器。在on函数中它包含如下数据:

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

见(documentation

它不使用Callback要求。但在某些情况下非常有用(例如在ajax调用中)

0

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

这里是一个的jsfiddle来说明:

http://jsfiddle.net/larryjoelane/qwawg9u4/6/

下面是小提琴的JavaScript代码:

 $(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); 

    }); 

     }); 
     }); 
    }); 

下面是实施例HTML:

  <div class="parent"> 

        I am the parent content 

       <div class="alert-close"> 

        hello im the alert close div container, click on me 

       </div> 
     </div> 
相关问题