jquery on click 事件调用命名函数 [英] jquery on click event that call a named function

查看:18
本文介绍了jquery on click 事件调用命名函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 jQuery onClick 处理程序,用这样的匿名函数编写:

I have a jQuery onClick handler, writed with an anonymous function like that:

$("#selector").on("click" function(){
    // do something
})

我会概括匿名函数,提取命名函数中的逻辑,这让我想到了类似的东西:

I would generalize the anonymous function extracting the logic in a named function, that drive me to something like:

$("#selector").on("click" namedFunction())

function namedFunction(){
    // do something
}

对我来说似乎是一个很好的解决方案.但是有一个缺点,因为 namedFunction 会在脚本加载后立即执行.这里您可以测试不良行为.

To me seemed a good solution. But there's a drawback since the namedFunction is executed as soon script is loaded. Here you can test the bad behaviour.

推荐答案

只需传递该函数本身的 reference.

Just pass the reference of that function itself.

试试,

$("#selector").on("click", namedFunction);

这篇关于jquery on click 事件调用命名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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