.andSelf()

.andSelf()

.andSelf()Returns: jQueryversion deprecated: 1.8, removed: 3.0

Description: Add the previous set of elements on the stack to the current set.

  • version added: 1.2.andSelf()

    • This method does not accept any arguments.

Note: This function has been deprecated and is now an alias for -addBack(), which should be used with jQuery 1-8 and later-

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, .andSelf() can help.

Consider a page with a simple list on it:

<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:

$( "li.third-item" ).nextAll().andSelf()
  .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 .andSelf() invocation merges these two sets together, creating a jQuery object that points to all three items in document order: [ <li.third-item>, <li>, <li> ].

Example:

The .andSelf() method causes the previous set of DOM elements in the traversal stack to be added to the current set. In the first example, the top stack contains the set resulting from .find("p"). In the second example, .andSelf() adds the previous set of elements on the stack — in this case $( "div.after-andself" ) — to the current set, selecting both the div and its enclosed paragraphs.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>andSelf demo</title>
  <style>
  p, div {
    margin: 5px;
    padding: 5px;
  }
  .border {
    border: 2px solid red;
  }
  .background {
    background: yellow;
  }
  .left, .right {
    width: 45%;
    float: left;
  }
  .right {
    margin-left: 3%;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js" rel="external nofollow" ></script>
</head>
<body>
 
<div class="left">
  <p><strong>Before <code>andSelf()</code></strong></p>
  <div class="before-andself">
    <p>First Paragraph</p>
    <p>Second Paragraph</p>
  </div>
</div>
<div class="right">
  <p><strong>After <code>andSelf()</code></strong></p>
  <div class="after-andself">
    <p>First Paragraph</p>
    <p>Second Paragraph</p>
  </div>
</div>
 
<script>
$( "div.left, div.right" ).find( "div, div > p" ).addClass( "border" );
 
// First Example
$( "div.before-andself" ).find( "p" ).addClass( "background" );
 
// Second Example
$( "div.after-andself" ).find( "p" ).andSelf().addClass( "background" );
</script>
 
</body>
</html>

Demo:

© The jQuery Foundation and other contributors
Licensed under the MIT License.
https://api.jquery.com/andSelf

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部