jQuery .on()方法 - 将参数传递给事件处理函数 [英] jQuery .on() method - passing argument to event handler function

查看:78
本文介绍了jQuery .on()方法 - 将参数传递给事件处理函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本不起作用

 < script type =text / javascript> 

函数ADS(e){alert(e); }

$(document).ready(function(){
$(document).on(dblclick,#an_tnam tr,ADS('hello'));
$(document).on(dblclick,#kv_tnam tr,ADS('world'));
// ....
});

< / script>

如何传递参数到事件处理函数ADS?

解决方案

.on()函数希望通过函数引用你正在做的是调用函数并传递其返回值。如果你需要传递一个参数,你需要把一个匿名函数打包出来。

  $(document) ('dblclick','#an_tnam tr',function(event){
ADS('hello');
});

jQuery总是将其归一化的事件对象作为第一个参数传递给要执行的函数。 >

I have the following script which does not work

<script type="text/javascript" >

   function ADS(e){ alert(e); }

   $(document).ready(function(){
          $(document).on("dblclick","#an_tnam tr", ADS('hello'));
          $(document).on("dblclick","#kv_tnam tr", ADS('world'));
          // ....  
 });

</script>

how can I pass argument to event handler function ADS ?

解决方案

The .on() function expects a function reference to be passed; what you're doing is calling the function and passing its return value. If you need to pass a parameter you'll need to wrap the call in an anonymous function.

$(document).on('dblclick', '#an_tnam tr', function(event) {
    ADS('hello');
});

jQuery always passes its normalized event object as the first argument to the function to be executed.

这篇关于jQuery .on()方法 - 将参数传递给事件处理函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆