jquery的on()绑定,事件分离怎么弄?求大神指点

yuanshai1991223 2013-05-08 06:28:04
比如,$(document).on('click','#id1',function(){
dosomething;
});
$(document).on('click','#id2',function(){
dosomething;
});
这样看上去不是很舒服,能不能写成
$(document).on('click','#id1',functin1());
$(document).on('click','#id2',functin2());
var function1 = 。。。;var function2=..;
..的形式,我直接把something提取成function显示has no method 'apply'错误,不是很懂js的面向对象机制,求指点一下二
...全文
112 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuanshai1991223 2013-05-08
  • 打赏
  • 举报
回复
好吧 我发现在 function1 里面 this竟然是指向dom对象的 看来还是对js了解的太少了啊
yuanshai1991223 2013-05-08
  • 打赏
  • 举报
回复
引用 2 楼 weilimicrver 的回复:
$(document).on('click','#id1',functin1); 去掉括号就是了
可是还是提示我Uncaught TypeError: Object #id1 has no method 'apply',我的问题是怎么在function1中获取事件点击的dom对象
天际的海浪 2013-05-08
  • 打赏
  • 举报
回复
应该这样 var function1 = function () { }; var function2 = function () { }; $(document).on('click','#id1',functin1); $(document).on('click','#id2',functin2);
JavaScript_R 2013-05-08
  • 打赏
  • 举报
回复
$(document).on('click','#id1',functin1); 去掉括号就是了
天际的海浪 2013-05-08
  • 打赏
  • 举报
回复
$(document).on('click','#id1',functin1); $(document).on('click','#id2',functin2); var function1 = function () { }; var function2 = function () { };

87,991

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧