.one()


.one( events [, data ], handler )Возвращает: jQuery

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

  • Добавлен в версии: 1.1.one( events [, data ], handler )

    • events
      Тип: String
      A string containing one or more JavaScript event types, such as "click" or "submit," or custom event names.
    • data
      Тип: PlainObject
      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.7.one( events [, selector ] [, data ], handler )

    • events
      Тип: String
      One or more space-separated event types and optional namespaces, such as "click" or "keydown.myPlugin".
    • selector
      Тип: String
      A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element.
    • data
      Тип: Anything
      Data to be passed to the handler in event.data when an event is triggered.
    • handler
      Тип: Function( Event eventObject )
      A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false.
  • Добавлен в версии: 1.7.one( events [, selector ] [, data ] )

    • events
      Тип: PlainObject
      An object in which the string keys represent one or more space-separated event types and optional namespaces, and the values represent a handler function to be called for the event(s).
    • selector
      Тип: String
      A selector string to filter the descendants of the selected elements that will call the handler. If the selector is null or omitted, the handler is always called when it reaches the selected element.
    • data
      Тип: Anything
      Data to be passed to the handler in event.data when an event occurs.

The .one() method is identical to .on(), except that the handler for a given element and event type is unbound after its first invocation. For example:

1
2
3
$( "#foo" ).one( "click", function() {
alert( "This will be displayed only once." );
});

After the code is executed, a click on the element with ID foo will display the alert. Subsequent clicks will do nothing. This code is equivalent to:

1
2
3
4
$( "#foo" ).on( "click", function( event ) {
alert( "This will be displayed only once." );
$( this ).off( event );
});

In other words, explicitly calling .off() from within a regularly-bound handler has exactly the same effect.

If the first argument contains more than one space-separated event types, the event handler is called once for each event type.

1
2
3
$( "#foo" ).one( "click mouseover", function() {
alert( "The " + event.type + " event happened!" );
});

In the example above the alert could be displayed twice due to the two event types (click and mouseover).

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