.delegate()


.delegate( selector, eventType, handler )Возвращает: jQueryversion deprecated: 3.0

Описание: Устанавливает обработчик события на элементы, соответствующие заданному селектору.

  • Добавлен в версии: 1.4.2.delegate( selector, eventType, handler )

    • selector
      Тип: String
      A selector to filter the elements that trigger the event.
    • eventType
      Тип: String
      A string containing one or more space-separated JavaScript event types, such as "click" or "keydown," or custom event names.
    • handler
      Тип: Function( Event eventObject )
      A function to execute at the time the event is triggered.
  • Добавлен в версии: 1.4.2.delegate( selector, eventType, eventData, handler )

    • selector
      Тип: String
      A selector to filter the elements that trigger the event.
    • eventType
      Тип: String
      A string containing one or more space-separated JavaScript event types, such as "click" or "keydown," or custom event names.
    • eventData
      Тип: Anything
      An object containing data that will be passed to the event handler.
    • handler
      Тип: Function( Event eventObject )
      A function to execute at the time the event is triggered.
  • Добавлен в версии: 1.4.3.delegate( selector, events )

    • selector
      Тип: String
      A selector to filter the elements that trigger the event.
    • events
      Тип: PlainObject
      A plain object of one or more event types and functions to execute for them.

As of jQuery 3.0, .delegate() has been deprecated. It was superseded by the .on() method since jQuery 1.7, so its use was already discouraged. For earlier versions, however, it remains the most effective means to use event delegation. More information on event binding and delegation is in the .on() method. In general, these are the equivalent templates for the two methods:

1
2
3
4
// jQuery 1.4.3+
$( elements ).delegate( selector, events, data, handler );
// jQuery 1.7+
$( elements ).on( events, selector, data, handler );

For example, the following .delegate() code:

1
2
3
$( "table" ).delegate( "td", "click", function() {
$( this ).toggleClass( "chosen" );
});

is equivalent to the following code written using .on():

1
2
3
$( "table" ).on( "click", "td", function() {
$( this ).toggleClass( "chosen" );
});

To remove events attached with delegate(), see the .undelegate() method.

Passing and handling event data works the same way as it does for .on().

Дополнительные замечания:

  • Since the .live() method handles events once they have propagated to the top of the document, it is not possible to stop propagation of live events. Similarly, events handled by .delegate() will propagate to the elements to which they are delegated; event handlers bound on any elements below it in the DOM tree will already have been executed by the time the delegated event handler is called. These handlers, therefore, may prevent the delegated handler from triggering by calling event.stopPropagation() or returning false.

Примеры использования