最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to pass parameters (other than event) to event handlers? - Stack Overflow

programmeradmin0浏览0评论

The following code renders 3 buttons with label "1", "2" and "3". Clicking on each button will alert the label.

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src=".min.js"></script>
        <script type="text/javascript">
            $(function() {
                var a = [1, 2, 3];
                $.each(a, function(i, ai) {
                    $('<button />').text(i).appendTo('body')
                        .click(function() {alert(i);});
                });
            });
        </script>
    </head>
    <body>
    </body>
</html>

However, if I replace function() {alert(i);} with foo and define function foo() { alert(i); }, I will get an error of variable i is not defined.

So how to pass parameters (other than event) to event handlers? I think defining the event handler (foo() in this case) as a named function will make the code cleaner if the event handler is long and plicated.

The following code renders 3 buttons with label "1", "2" and "3". Clicking on each button will alert the label.

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis./ajax/libs/jquery/1/jquery.min.js"></script>
        <script type="text/javascript">
            $(function() {
                var a = [1, 2, 3];
                $.each(a, function(i, ai) {
                    $('<button />').text(i).appendTo('body')
                        .click(function() {alert(i);});
                });
            });
        </script>
    </head>
    <body>
    </body>
</html>

However, if I replace function() {alert(i);} with foo and define function foo() { alert(i); }, I will get an error of variable i is not defined.

So how to pass parameters (other than event) to event handlers? I think defining the event handler (foo() in this case) as a named function will make the code cleaner if the event handler is long and plicated.

Share Improve this question asked Sep 22, 2010 at 6:39 powerboypowerboy 11k21 gold badges65 silver badges94 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

If you look at the documentation for bind, you'll see it has an optional eventData parameter. So for example this would work:

function foo(e)
{
    alert(e.data.theI);
}

$(function ()
{
    var a = [1, 2, 3];
    $.each(a, function (i, ai)
    {
        $('<button/>').text(i).appendTo('body').bind("click", {theI: i}, foo);
    });
});
$(function() {
                var a = [1, 2, 3];
                $.each(a, function(i, ai) {
                    $('<button />').text(i).appendTo('body')
                        .click(function() { foo.apply(this,[i]);});
                });
            });


function foo( i )
{
   alert( i + " : " + $(this).text() );
}

A third method, and the way I usually do it is to invoke a function that returns your handler. Like this:

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis./ajax/libs/jquery/1/jquery.min.js"></script>
        <script type="text/javascript">
            var bindFoo = function(x) {
                return function() {alert(x);};
            };
            $(function() {
                var a = [1, 2, 3];
                $.each(a, function(i, ai) {
                    $('<button />').text(i).appendTo('body')
                        .click(bindFoo(i));
                });
            });
        </script>
    </head>
    <body>
    </body>
</html>

I'm using x in the binding function only to distinguish it from i in the main code block.

发布评论

评论列表(0)

  1. 暂无评论