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

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

问题描述

我有以下脚本不起作用

<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>

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

how can I pass argument to event handler function ADS ?

推荐答案

.on() 函数需要传递一个函数引用;你正在做的是调用函数并传递它的返回值.如果需要传递参数,则需要将调用包装在匿名函数中.

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 总是将其规范化的事件对象作为第一个参数传递给要执行的函数.

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

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

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