.addBack()


.addBack( [selector ] )Возвращает: jQuery

Описание: Функция объединяет предыдущую выборку элементов с текущей. Это часто необходимо в тех случаях, когда Вам нужно осуществить над этими элементами общее действие.

  • Добавлен в версии: 1.8.addBack( [selector ] )

    • selector
      Тип: Selector
      A string containing a selector expression to match the current set of elements against.

As described in the discussion for .end(), jQuery objects maintain an internal stack that keeps track of changes to the matched set of elements. When one of the DOM traversal methods is called, the new set of elements is pushed onto the stack. If the previous set of elements is desired as well, .addBack() can help.

Consider a page with a simple list on it:

1
2
3
4
5
6
7
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li class="third-item">list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
</ul>

The result of the following code is a red background behind items 3, 4 and 5:

1
2
$( "li.third-item" ).nextAll().addBack()
.css( "background-color", "red" );

First, the initial selector locates item 3, initializing the stack with the set containing just this item. The call to .nextAll() then pushes the set of items 4 and 5 onto the stack. Finally, the .addBack() invocation merges these two sets together, creating a jQuery object that points to all three items in document order: {[<li.third-item>,<li>,<li> ]}.

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