Can you set event.data with jquery trigger

后端 未结 11 559
轻奢々
轻奢々 2020-12-12 16:27

With jQuery .on() you can pass an optional parameter to set the event data. Can you do this with trigger as well?

11条回答
  •  情深已故
    2020-12-12 16:55

    In jQuery site you can see the declaration for the trigger function: .trigger( eventType [, extraParameters] )

    The extraParameters must be an array or a single object and you will be allowed to get these objects with arguments[1+] (arguments[0] is equal to event object).

    So, here's an example:

    $('#foo').bind('custom', function(event) {
      if ( arguments.length > 1 ) {
        $.each(arguments, function(i,arg){
          alert("element " + i + " is " + arg);
        })
      }
    });
    
    $('#foo').trigger('custom', ['Custom', 'Event', { 'test' : 'attr from object' }, 123]);
    

提交回复
热议问题