将参数传递给调用的事件处理程序,即 element.onchange(); javascript

发布于 2024-11-24 12:35:32 字数 446 浏览 1 评论 0原文

我有一个这样的函数:

function doSomething()
{
  // do something with select element
}

document.getElementById("selectel").onchange = doSomething;

// Call onchange event
document.getElementById("selectel").onchange();

现在,我认识到我可以直接调用该函数并传递参数。但我想知道是否可以在调用 onchange() 事件处理程序后将参数传递给它。我尝试过

document.getElementById("selectel").onchange("hello");

,但这没有用。

感谢您的帮助。

I have a function like this:

function doSomething()
{
  // do something with select element
}

document.getElementById("selectel").onchange = doSomething;

// Call onchange event
document.getElementById("selectel").onchange();

Now, I recognize that I could call the function directly and pass a parameter. But I'd like to know if it's possible to pass a parameter to the onchange() event handler after it's evoked. I tried

document.getElementById("selectel").onchange("hello");

, but this didn't work.

Thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只是我以为 2024-12-01 12:35:32

您需要将参数绑定到您的函数。我将从 Ext-JS 复制粘贴一个函数,让您可以做到这一点。 警告:不适合初学者

/**
 * Create a new function from the provided <code>fn</code>, change <code>this</code> to the provided scope, optionally
 * overrides arguments for the call. (Defaults to the arguments passed by the caller)
 *
 * @param {Function} fn The function to delegate.
 * @param {Object} scope (optional) The scope (<code><b>this</b></code> reference) in which the function is executed.
 * <b>If omitted, defaults to the browser window.</b>
 * @param {Array} args (optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller)
 * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
 * if a number the args are inserted at the specified position
 * @return {Function} The new function
 */
function bind(fn, scope, args, appendArgs) {
    var method = fn,
        applyArgs;

    return function() {
        var callArgs = args || arguments;

        if (appendArgs === true) {
            callArgs = Array.prototype.slice.call(arguments, 0);
            callArgs = callArgs.concat(args);
        }
        else if (typeof appendArgs == 'number') {
            callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
            applyArgs = [appendArgs, 0].concat(args); // create method call params
            Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
        }

        return method.apply(scope || window, callArgs);
    };
}

您可以像调用处理程序时一样使用它

function onChange(e, customParameter) {
  // Whatever code
}

document.getElementById("selectel").onchange = bind(onChange, null, ["customParameter"], true);

,附加参数将附加到事件处理程序(事件对象)传递的参数中。

这个函数有很多内容,所以请随时提出任何其他问题。

这是一个 jsfiddle,可以看到它的实际效果 http://jsfiddle.net/yBhG6/

You need to bind a parameter to your function. I'm going to copy paste a function from Ext-JS that lets you do just that. Warning: not for beginners

/**
 * Create a new function from the provided <code>fn</code>, change <code>this</code> to the provided scope, optionally
 * overrides arguments for the call. (Defaults to the arguments passed by the caller)
 *
 * @param {Function} fn The function to delegate.
 * @param {Object} scope (optional) The scope (<code><b>this</b></code> reference) in which the function is executed.
 * <b>If omitted, defaults to the browser window.</b>
 * @param {Array} args (optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller)
 * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
 * if a number the args are inserted at the specified position
 * @return {Function} The new function
 */
function bind(fn, scope, args, appendArgs) {
    var method = fn,
        applyArgs;

    return function() {
        var callArgs = args || arguments;

        if (appendArgs === true) {
            callArgs = Array.prototype.slice.call(arguments, 0);
            callArgs = callArgs.concat(args);
        }
        else if (typeof appendArgs == 'number') {
            callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
            applyArgs = [appendArgs, 0].concat(args); // create method call params
            Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
        }

        return method.apply(scope || window, callArgs);
    };
}

You can use it like

function onChange(e, customParameter) {
  // Whatever code
}

document.getElementById("selectel").onchange = bind(onChange, null, ["customParameter"], true);

When your handler is called, additional parameters are appended to the arguments passed by the event handler (the event object).

There's a lot of meat in this function, so feel free to ask any additional questions.

Here's a jsfiddle to see it in action http://jsfiddle.net/yBhG6/

故事和酒 2024-12-01 12:35:32

声明一个匿名函数:

document.getElementById("selectel").onchange = function() { doSomething("hello"); }

Declare an anonymous function:

document.getElementById("selectel").onchange = function() { doSomething("hello"); }
森林散布 2024-12-01 12:35:32

您可以使用 apply() 方法来传递参数。

doSomething.apply(document.getElementById("selectel"), "hello");

You could use the apply() method which lets you pass arguments.

doSomething.apply(document.getElementById("selectel"), "hello");
惟欲睡 2024-12-01 12:35:32

我可以看到解决这个问题的两种方法。

一种是通过将 select 传递为 this 来直接调用回调:

doSomething.apply(document.getElementById("selectel"), "Hello");

第二种与 Igor 类似,但需要其他变量的帮助:

var param = "foo"; // Whatever default is
document.getElementById("selectel").onchange = function() {
    doSomething(param);
} 

param = "hello";
document.getElementById("selectel").onchange();

I can see two approach to this question.

One is to call Your callback directly by passing select as this:

doSomething.apply(document.getElementById("selectel"), "Hello");

Second is similar to Igor's, but with help of other variables:

var param = "foo"; // Whatever default is
document.getElementById("selectel").onchange = function() {
    doSomething(param);
} 

param = "hello";
document.getElementById("selectel").onchange();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文