简体   繁体   English

jQuery如何为匿名函数中的get参数定义click函数?

[英]How jquery define click function for get parameter inside anonymous function?

I dont understand javascript syntax well,my question: 我不太了解javascript语法,我的问题是:

How jquery define click function for get parameter inside anonymous function? jQuery如何为匿名函数中的get参数定义click函数?

The Case: 案子:

$("a").click(function(event) { 
    alert(event.type); 
});

in C the function should be defined: 在C中,应定义函数:

void click(fn,event){
}

in javascript its looks to me that she defined as- (but where defined event?): 在javascript中,对我来说,她将其定义为-(但在哪里定义了事件?):

click (fn){

}

please explain to me the jquery syntax of click function code source here . 在这里给我解释点击功能代码源的jQuery语法。

Thanks, Yosef 谢谢,约瑟夫

If you just want to find out where the event object is passed to your handler, that would be line 2568 of the jQuery-1.5.2 redistributable source code (or line 438 of the actual, un-contatenated source file ): 如果您只想查找event对象在哪里传递给您的处理程序,那将是jQuery-1.5.2可再分发源代码的 2568 (或实际的,不受污染的源文件的438行 ):

var ret = handleObj.handler.apply( this, args );

In the above line of code, handler is your anonymous function and args is an array whose first element is the event object. 在上面的代码行中, handler是您的匿名函数,而args是一个数组,其第一个元素是事件对象。 jQuery uses the apply method of the JavaScript Function object to invoke the handler and pass in the arguments jQuery使用JavaScript Function对象的apply方法调用处理程序并传递参数

The jQuery source code is quite complex when it comes to full sequence of adding and handling events so, unless you want a line-by-line explanation of hundreds of lines of code, I suggest you rephrase your question to a smaller scope (eg You could create a toy demonstration of the scenario you want to understand). 当涉及到添加和处理事件的全部序列时,jQuery源代码非常复杂,因此,除非您要逐行解释数百行代码,否则建议将问题改写为较小的范围(例如,可以创建您想了解的场景的玩具演示)。

Perhaps this will help? 也许这会有所帮助?

dosomething(function(message) {
    alert(message);    
});

function dosomething(fn) {
    fn("Hello!");
}

The first part of the jQuery is the selector $("a") which selects and returns object(s) selected from the DOM. jQuery的第一部分是选择器$("a") ,它选择并返回从DOM中选择的对象。 In this case, it will return a list of all anchor tag objects on the page. 在这种情况下,它将返回页面上所有锚标记对象的列表。

Then, you are chaining the .click() method to that, jQuery attaches an event listener to all of the anchor tags. 然后,您将.click()方法链接到该方法,jQuery将事件侦听器附加到所有锚标记。 When the event listener is attached, it is more or less the equivalent of doing 附加事件侦听器后,它或多或少相当于

<a href='..' onclick='someFunction(event)'>some link</a>

...which passes the event object to the function. ...将事件对象传递给函数。

For example, compare to this: 例如,与此进行比较:

<a onclick='blah(event)'>click</a>
<script type='text/javascript'>
function blah(e) {
  alert (e.type);
}
</script>

If I click on it, I will see "click" in the alert. 如果单击它,我将在警报中看到“单击”。 In principle, jQuery is doing the same thing. 原则上,jQuery在做同样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM