调用 JQuery bootstrap 事件参数

huangapple go评论55阅读模式
英文:

Call JQuery bootstrap events parameters

问题

When I call an event using jQuery, how can I know that the function has parameters or not, and what can I parse to those parameters. Example:

$("#id").on('show.bs.modal', function () { });
$("#id").on('show.bs.modal', function (e) { });

在上面的示例中,我传递了参数e。上述两种语法都可以正常工作。

英文:

When I call an event using jquery, how can I know that the function has parameters or not, and what can I parse to those parameters. Example

$("#id").on('show.bs.modal', function () { });
$("#id").on('show.bs.modal', function (e) { });

In the example above, I passed the parameter e. The two syntax above can work well.

答案1

得分: 0

使用jQuery的on方法绑定事件处理程序时,您可以通过检查处理程序函数接收的参数数量来确定该函数是否具有参数。您可以使用arguments.length属性来获取传递给函数的参数数量。

以下是一个示例,演示了如何检查事件处理程序函数是否具有参数:

$("#id").on('show.bs.modal', function () {
    if (arguments.length > 0) {
        // 处理程序具有参数
        var e = arguments[0];
        // 根据需要访问和使用参数(e)
    } else {
        // 处理程序没有参数
    }
});
英文:

When using jQuery's on method to bind an event handler, you can determine whether the function has parameters or not by checking the number of arguments received by the handler function. You can use the arguments.length property to get the number of arguments passed to the function.

Here's an example of how you can check if the event handler function has parameters:


$("#id").on('show.bs.modal', function () {
    if (arguments.length > 0) {
        // The handler has parameters
        var e = arguments[0];
        // Access and use the parameter (e) as needed
    } else {
        // The handler does not have parameters
    }
});

huangapple
  • 本文由 发表于 2023年6月29日 13:50:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定