如何将额外的参数传递给事件处理回调? [英] How to pass extra parameter to event handling callback?

查看:86
本文介绍了如何将额外的参数传递给事件处理回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个想要附加事件监听器的按钮。我还需要向此函数传递一个额外的参数URL。我阅读了有关 apply 的信息,并且正在执行以下操作:

I have a button on which I want to attach an event listener. I also need to pass a extra parameter url to this function. I read about apply and I'm doing the following:

$('#list-button').on('click',postListing.apply([url]));

我的问题是,一旦加载此脚本, postListing 被调用。我没有在其他任何地方调用该函数。我需要仅在单击时调用它。

My problem is that as soon as this script is loaded postListing is called. I am not calling the function anywhere else. I need it to be called only on click.

推荐答案

bind和call / apply之间的区别是bind不调用该函数立即就像在需要时使用变量加载数据一样

The difference between bind and call/apply is that bind doesn't call the function immediately much like it loads the data with the variable when needed

您可以重新格式化代码,使其看起来像这样

You can reformat your code so it looks like this

$('#list-button').on('click', postListing.bind(this, url));

这篇关于如何将额外的参数传递给事件处理回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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