简体   繁体   English

单击元素时如何执行外部函数?

[英]How can I execute an external function when an element is clicked?

I'm trying to execute an external function on click of a DOM element without wrapping it in another function. 我试图在点击DOM元素时执行外部函数,而不将其包装在另一个函数中。

Say I have a function called sayHello() , as follows: 假设我有一个名为sayHello()的函数,如下所示:

function sayHello(){
  alert("hello");
};

To execute it on click, I currently have to do this: 要在点击时执行它,我目前必须这样做:

$("#myelement").click(function(){
  sayHello();
});

Notice I am forced to wrap the single function call in yet another function. 注意我被迫将单个函数调用包装在另一个函数中。 What I am trying to do is something like this 我想要做的就是这样

$("#myelement").click(sayHello());

Except that simply doesn't work. 除此之外根本不起作用。 Can I avoid wrapping the single function call in another function in any way? 我可以避免以任何方式将单个函数调用包装在另一个函数中吗? Thanks! 谢谢!

.

Additional information: How would I achieve the same thing when I need to pass parameters to the function? 附加信息:当我需要将参数传递给函数时,我将如何实现相同的功能?

.. ..

Additional information: Like Chris Brandsma and Jani Hartikainen pointed out, one should be able to use the bind function to pass parameters to the function without wrapping it in another anonymous function as such: 附加信息:像Chris Brandsma和Jani Hartikainen所指出的那样,应该能够使用bind函数将参数传递给函数而不将其包装在另一个匿名函数中:

$("#myelement").bind("click", "john", sayHello);

with sayHello() now accepting a new parameter, as such: sayHello()现在接受一个新参数,如下:

function sayHello(name){
  alert("Hello, "+name);
}

This, unfortunately, does not seem to work... Any ideas? 不幸的是,这似乎不起作用......任何想法? The Events/bind documentation is located here Thanks! 事件/绑定文档位于此处谢谢!

To pick up from a question you had in there. 从你在那里的问题中挑选。

Obviously, you need to call it this way to work: 显然,你需要以这种方式调用它:

$("#myelement").click(sayHello);

Calling it the way you had it actually executes the method instead of sending the method to the click handler. 以它的方式调用它实际上执行方法而不是将方法发送到单击处理程序。

If you need to pass data you can also call the method this way: 如果您需要传递数据,您也可以这样调用方法:

$("#myelement").click(function(){ sayHello("tom");});

Or you can pass data via the bind() function 或者您可以通过bind()函数传递数据

function sayHello(event){ alert("Hello, "+event.data); }

$("#myelement").bind("click", "tom", sayHello);

Or you can retrieve data from the element that was clicked 或者,您可以从单击的元素中检索数据

$("#myelement").click(function(){sayHello($(this).attr("hasData");});.

Hope that helps. 希望有所帮助。

You are calling the sayHello-function, you can pass it by reference by removing the parenthesis: 您正在调用sayHello函数,您可以通过删除括号来引用它:

$("#myelement").click(sayHello);

Edit: If you need to pass parameters to the sayHello-function, you will need to wrap it in another function. 编辑:如果需要将参数传递给sayHello函数,则需要将其包装在另一个函数中。 This defines a parameter-less new function that calls your function with the parameters provided at creation-time: 这定义了一个无参数的新函数,它使用创建时提供的参数调用函数:

$("#myelement").click(function () {
   sayHello('name');
   // Creates an anonymous function that is called when #myelement is clicked.
   // The anonymous function invokes the sayHello-function
});

$("#myelement").click(sayHello()); $( “#myelement”)点击(的sayHello());

This is actually calling sayHello() before you set the click handler. 这实际上是在设置click处理程序之前调用sayHello() It's trying to set the return value of sayHello() as the callback function! 它试图将sayHello()的返回值设置为回调函数!

Instead, try: 相反,尝试:

$("#myelement").click(sayHello);

And regarding your edit, if you need to pass parameters you can just use the closure technique you're already using, but if you're looking for something cleaner then check out How can I pass a reference to a function, with parameters? 关于你的编辑,如果你需要传递参数,你可以使用你已经使用的闭包技术,但如果你正在寻找更清洁的东西,那么看看如何通过参数传递对函数的引用?

what params would you be passing at the time of the click? 点击时你会传递什么参数? if you know what they are in advance, you can set them before the user clicks. 如果你事先知道它们是什么,你可以在用户点击之前设置它们。

you can't pass parameters but you can fake the parameter thing by simply adding members to the object which is firing the event, so.. 你不能传递参数,但你可以通过简单地将成员添加到触发事件的对象来伪造参数,所以..

myObject.onClick = sayHello;
myObject.param1 = "foo";

then, when you're calling this 然后,当你打电话给这个

function sayHello(){
  alert(this.param1);
}

The bind() method is now deprecated. 现在不推荐使用bind()方法。

As of jQuery 3.0, .bind() has been deprecated. 从jQuery 3.0开始, .bind()已被弃用。 It was superseded by the .on() method for attaching event handlers to a document since jQuery 1.7, so its use was already discouraged. 它被.on()方法取代,因为自jQuery 1.7以来将事件处理程序附加到文档,因此不鼓励使用它。

You are better off doing all event binding with the on() method for consistency. 最好使用on()方法进行所有事件绑定以保持一致性。 You can pass parameters to your handler as the second argument after the event name. 您可以将参数作为事件名称后面的第二个参数传递给处理程序。

function sayHello(name){
    console.log('Hello ' + name);
};

$('#myelement').on('click', 'John', sayHello);

是的,而不是传递sayHello(),只需传递函数名称。

$("#myelement").click(sayHello);

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

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