简体   繁体   English

传递给.click()方法参数的参数是否始终是Event对象?

[英]Is the argument passed to .click() method parameter always the Event object?

I just want to understand how the parameter in the .click() method works. 我只想了解.click()方法中的参数的工作方式。 From my understanding, the .click() method takes a function as a parameter. 据我了解,.click()方法采用函数作为参数。

As per the jQuery API, .click() can take two parameters: 根据jQuery API,.click()可以采用两个参数:

([eventData], event handler)

And then the event handler function can take this parameter: 然后事件处理程序函数可以采用以下参数:

(Event eventObject) 

Does that mean that the function parameter of the .click() method would always expect the "Event object" to be passed to it? 这是否意味着.click()方法的功能参数始终希望将“事件对象”传递给它?

Like, for the below example, I created a random object and then passed it as an argument. 像下面的示例一样,我创建了一个随机对象,然后将其作为参数传递。 The function parameter of .click() method seems to ignore that it is a random object and continued to use it as the "Event object". .click()方法的功能参数似乎忽略了它是一个随机对象,并继续将其用作“事件对象”。

var anObject = {
  "property1" : "someproperty"
};

$(document).click(function(anObject) {
  var x = anObject.pageX;
  var y = anObject.pageY;

  logClicks(x, y);
});

var logClicks = function(x, y) {
  console.log("x: " + x + " y: " + y)
}

The question is, what is going on under the hood? 问题是,到底发生了什么? Why is it the case? 为什么会这样呢?

EDIT: 编辑:

I'm not so confused about the [eventData] parameter and how the event handler uses it. 我对[eventData]参数以及事件处理程序如何使用它并不感到困惑。 Just more on how its function parameter sort of converts any parameter you pass to it to be a variable that holds the Event object . 关于它的函数参数如何将传递给它的任何参数转换为保存Event对象的变量的更多信息

Does that mean that the function parameter of the .click() method would always expect the "Event object" to be passed to it? 这是否意味着.click()方法的功能参数始终希望将“事件对象”传递给它?

Yes, the parameter passed to the event handler function will always be a jQuery event object. 是的,传递给事件处理函数的参数将始终是jQuery事件对象。

Your confusion seems to be around the ([eventData], event handler) signature of the click() method. 您的困惑似乎与click()方法的([eventData], event handler)签名有关。 The first parameter in this case is used to add a value to the data parameter of the event object provided to the event handler. 在这种情况下,第一个参数用于将值添加到提供给事件处理程序的event对象的data参数。 In the case of the code in your question, you would use this: 对于您问题中的代码,您将使用以下代码:

 var anObject = { "property1" : "someproperty" }; $(document).click(anObject, function(e) { var x = e.pageX; var y = e.pageY; var data = e.data.property1; logClicks(data, x, y); }); var logClicks = function(data, x, y) { console.log(data, x, y) } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

You're passing an anonymous function as a parameter to the .click() function. 您正在将匿名函数作为参数传递给.click()函数。

Hence that function has a different scope and anObject is not what you've globally declared but what is actually passed by the jQuery API which is an Event. 因此,该函数具有不同的作用域,并且anObject不是您全局声明的对象,而是由jQuery API(事件)实际传递的对象。

var anObject = {
  "property1" : "someproperty"
};

$(document).click(function aNewFunctionWithHisOwnScope(anObject) {
  // here anObject references an Event
  x = anObject.pageX;
  y = anObject.pageY;

  logClicks(x, y);
});

// here anObject references again your defined anObject

So finally, yes: It will always be an Event 最后,是的:这将永远是一个事件

EDIT since I might haven't been clear: 编辑,因为我可能还不清楚:

your anObject var is available from inside the anonymous function since it's like a global variable. 您的anObject变量可从匿名函数内部获得,因为它就像一个全局变量。 But since they've the same name , inside the function, the last reference is used, hence the value used (inside the function) is the one passed as parameter, while outside the function the 'global' anObject keeps its value. 但是由于它们具有相同的名称 ,因此在函数内部使用了最后一个引用,因此(在函数内部)使用的值是作为参数传递的值,而在函数外部,“全局” anObject保留其值。

The first parameter in the click([eventData], event handler) needs to be eventData which will be further passed to the event handler and later you can access that data using event.data : click([eventData], event handler)的第一个参数需要为eventData ,它将进一步传递给事件处理程序,稍后您可以使用event.data访问该数据:

$(document).click(anObject, function(e) {
  x = e.pageX;
  y = e.pageY;

  // access the object passed as e.data
  console.log(e.data.property1);

  logClicks(x, y);
});

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

相关问题 作为参数传递的对象的JavaScript调用方法 - JavaScript call method of object passed as argument 当作为参数传递时,对象方法回调在事件处理程序中失去其绑定,但在硬编码时不会 - Object-method callback loses its binding in an event handler when passed as a parameter, but not when hard-coded 事件对象/参数是否会自动传递给 React 事件处理程序? - Does event object / argument get passed automatically to React event handlers? Object 作为参数传递给 function 时变为未定义,方法称为 JavaScript - Object turns into undefined when passed to function as argument and method is called JavaScript 修改作为方法内部参数传递的对象是一种不好的做法吗? - is it a bad practice modifing an object passed as an argument inside a method? 返回传递的参数作为对象 - Returning passed argument as object 事件参数未传递给JavaScript函数 - Event argument not passed to JavaScript function 如何从传递给“ on”方法的事件处理程序访问“ $(this)”对象? - how to access to “$(this)” object from event handler passed to “on” method? JS总是通过mousedown获得相同的事件对象“ e”参数 - JS Always getting the same event object “e” parameter with mousedown 点击事件并非总是触发 - Click event not always triggering
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM