简体   繁体   English

如何在jQuery中将参数传递给事件处理程序?

[英]How can I pass arguments to event handlers in jQuery?

With jQuery code like: 使用类似jQuery的代码:

$("#myid").click(myfunction);

function myfunction(arg1, arg2) {/* something */}

How do I pass arguments to myfunction while using jQuery? 使用jQuery时如何将参数传递给myfunction

The simplest way is to do it like so (assuming you don't want any of the event information passed to the function)... 最简单的方法是这样做(假设您不希望将任何事件信息传递给该函数)...

$("#myid").click(function() {
    myfunction(arg1, arg2);
});

jsFiddle . jsFiddle

This create an anonymous function, which is called when the click event is triggered. 这将创建一个匿名函数,在click事件触发时将调用该函数。 This will in turn call myfunction() with the arguments you provide. 这将依次使用您提供的参数调用myfunction()

If you want to keep the ThisBinding (the value of this when the function is invoked, set to the element which triggered the event), then call the function with call() . 如果你想保持ThisBinding的值( this当函数被调用时,设置为触发事件的元素),然后调用与函数call()

$("#myid").click(function() {
    myfunction.call(this, arg1, arg2);
});

jsFiddle . jsFiddle

You can't pass the reference directly in the way your example states, or its single argument will be the jQuery event object . 您不能以示例所陈述的方式直接传递引用,否则它的单个参数将是jQuery event object

If you do want to pass the reference, you must leverage jQuery's proxy() function (which is a cross browser wrapper for Function.prototype.bind() ). 如果确实要传递引用,则必须利用jQuery的proxy()函数(这是Function.prototype.bind()的跨浏览器包装)。 This lets you pass arguments, which are bound before the event argument. 这使您可以传递在event参数之前绑定的参数。

$("#myid").click($.proxy(myfunction, null, arg1, arg2));   

jsFiddle . jsFiddle

In this example, myfunction() would be executed with its ThisBinding intact ( null is not an object, so the normal this value of the element which triggered the event is used), along with the arguments (in order) arg1 , arg2 and finally the jQuery event object, which you can ignore if it's not required (don't even name it in the function's arguments). 在此示例中, myfunction()将以其ThisBinding完整无缺的方式执行( null不是对象,因此使用触发事件的元素的正常this值)以及参数arg1arg2以及最后一个参数jQuery event对象,如果不需要它,您可以忽略它(甚至不要在函数的参数中命名它)。

You could also use use the jQuery event object's data to pass data, but this would require modifying myfunction() to access it via event.data.arg1 (which aren't function arguments like your question mentions), or at least introducing a manual proxy function like the former example or a generated one using the latter example. 您还可以使用jQuery event对象的data来传递数据,但这需要修改myfunction()才能通过event.data.arg1 (不是像您提到的问题那样的函数参数 myfunction()来访问它,或者至少要引入手册代理功能,例如前一个示例,或使用后一个示例生成的功能。

$("#myid").on('click', {arg1: 'hello', arg2: 'bye'}, myfunction);

function myfunction(e) {

    var arg1 = e.data.arg1;
    var arg2 = e.data.arg2;

    alert(arg1);
    alert(arg2);

}

//call method directly:
myfunction({
    arg1: 'hello agian', 
    arg2: 'bye again'
});

Also allows you to bind and unbind specific event handlers using the on and off methods. 还允许您使用on和off方法来绑定和取消绑定特定的事件处理程序。

Example: 例:

$("#myid").off('click', myfunction);

This would unbind the myfunction handler from #myid 这将使MyFunction处理程序与#myid解除绑定

while you should certainly use Alex's answer, the prototype library's "bind" method has been standardized in Ecmascript 5, and will soon be implemented in browsers natively. 虽然您当然应该使用Alex的答案,但原型库的“绑定”方法已在Ecmascript 5中进行了标准化,并将很快在本机浏览器中实现。 It works like this: 它是这样的:

jQuery("#myid").click(myfunction.bind(this, arg1, arg2));

Old thread, but for search purposes; 旧线程,但出于搜索目的; try: 尝试:

$(selector).on('mouseover',...);

... and check out the "data" parameter: http://api.jquery.com/on/ ...并检查“数据”参数: http : //api.jquery.com/on/

eg: 例如:

function greet( event ) {
  alert( "Hello " + event.data.name );
}
$( "button" ).on( "click", {name: "Karl"}, greet );
$( "button" ).on( "click", {name: "Addy"}, greet );

There are great answers already, but anyway, here are my two cents. 已经有好答案了,但是无论如何,这是我的两分钱。 You can also use: 您还可以使用:

$("#myid").click({arg1: "foo", arg2: "bar"}, myfunction)

And the listener would look like: 监听器看起来像:

function myfunction(event){ alert(event.data.arg1); alert(event.data.arg2); }

Simple: 简单:

$(element).on("click", ["Jesikka"],  myHandler);

function myHandler(event){
   alert(event.data);     //passed in "event.data"
}

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

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