jQuery .on() method - passing argument to event handler function

前端 未结 6 1382
别那么骄傲
别那么骄傲 2021-01-31 07:49

I have the following script which does not work



        
相关标签:
6条回答
  • 2021-01-31 08:29

    As Anthony Grist pointed out, the .on() method is expecting a function reference at that part; you're evaluating a function which returns nothing (null).

    However, one fun feature of JavaScript is that everything is an object, including functions. With a small modification, you can change ADS() to return an anonymous function object instead:

    function ADS(e){ 
        return function(){ alert(e); };
    }
    

    http://jsfiddle.net/cSbWb/

    0 讨论(0)
  • 2021-01-31 08:31

    The .on() function expects a function reference to be passed; what you're doing is calling the function and passing its return value. If you need to pass a parameter you'll need to wrap the call in an anonymous function.

    $(document).on('dblclick', '#an_tnam tr', function(event) {
        ADS('hello');
    });
    

    jQuery always passes its normalized event object as the first argument to the function to be executed.

    0 讨论(0)
  • 2021-01-31 08:32
    function ADS(e){ alert(e); }
    
    $(document).ready(function(){
              $(document).on("dblclick","#an_tnam tr", function (e) { ADS('hello') });
    
     });
    

    will do the trick.

    0 讨论(0)
  • 2021-01-31 08:42

    You can pass extra data to an event handling function and can be accessed using event.data within the handler.

    $(document).on('dblclick', '#an_tnam tr', { extra : 'random string' }, function(event)
    {
        var data = event.data;
    
        // Prints 'random string' to the console
        console.log(data.extra);
    }
    

    You can also send extra data to any event you like when triggering the event from an external source using the .trigger() method

    $('#an_tnam tr').trigger('click', [{ extra : 'random string' }]);
    

    The difference with passing data to the trigger method is that it expects the handler to take extra arguments of the length of the array passed in. The above would expect the handler to have one extra argument to contain the object passed in.

    $('#an_tnam tr').on('click', function(event, obj)
    {
       // Prints 'random string' to the console
       console.log(obj.extra);
    }
    
    0 讨论(0)
  • 2021-01-31 08:50

    Actually, there is a very neat simple way to achieve this, with no extra clutter and no anonymous functions, using JS bind():

    $(document).on('dblclick', ADS.bind(null, 'hello'));
    

    First parameter is the value you want "this" to have inside callback function.

    MOre info in Mozilla Developer Network: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind

    0 讨论(0)
  • 2021-01-31 08:56
    function ADS(e) {
        return function() {
            alert(e);
        };
    }
    

    Like that when you're doing

    $(document).on("dblclick","#an_tnam tr", ADS('hello'));
    

    , it is the returned function that is assigned as event handler (and your string argument is passed when you're assigning the handler, not when it's called).

    0 讨论(0)
提交回复
热议问题