【问题标题】:Calling a function with jquery on? [closed]使用 jquery 调用函数? [关闭]
【发布时间】:2014-05-15 10:53:14
【问题描述】:

我希望使用 query 和 'on' 调用方法

我该怎么做?

我试过了:

$(img).on('mousedown', test());

【问题讨论】:

  • 不知道 img 是什么就很难回答
  • @panthro 明确更新您的要求
  • api.jquery.com/on阅读手册后您还有其他问题吗?

标签: jquery


【解决方案1】:

去掉回调函数中的括号

 $(":image").on('mousedown', test);

// 函数定义

function test(){
    alert("mouse is down");
    }

【讨论】:

  • $(img).on('mousedown', test); 是正确的,function test(){}
  • 我得到:未捕获的 ReferenceError:分配中的左侧无效
  • @panthro 你能用小提琴重现你的问题吗?
  • 您可能应该证明函数 test() 将在别处定义以使操作更清晰
【解决方案2】:

这是你需要的:

$(img).on('mousedown', function(){
test();
});

【讨论】:

  • 为什么要把事情复杂化?将函数定义替换为test,它在语法上是相等的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-23
  • 1970-01-01
  • 2016-07-17
相关资源
最近更新 更多