Home  >  Article  >  Web Front-end  >  What is the usage of end in jquery

What is the usage of end in jquery

藏色散人
藏色散人Original
2021-11-12 10:17:472179browse

The function of end in jquery is to end the latest filtering operation in the current chain and restore the matching element set to its previous state. Its usage syntax is ".end()".

What is the usage of end in jquery

The operating environment of this article: windows7 system, jquery3.2.1 version, DELL G3 computer

#What is the usage of end in jquery?

jquery end() method ends the most recent filter operation in the current chain and restores the set of matching elements to its previous state.

Syntax

.end()

Detailed description

Most jQuery traversal methods operate on a jQuery object instance and generate a new object that matches a different set of DOM elements. When this happens, the new set of elements should be pushed onto the stack maintained in the object. Each successful filter method call pushes a new element onto the stack. If we need the old set of elements, we can use end() to pop the new set from the stack.

Suppose there is a pair of short lists in the page:

<ul class="first">
   <li class="foo">list item 1</li>
   <li>list item 2</li>
   <li class="bar">list item 3</li>
</ul>
<ul class="second">
   <li class="foo">list item 1</li>
   <li>list item 2</li>
   <li class="bar">list item 3</li>
</ul>

Example

Mainly jQuery will be more useful when using jQuery's chain attribute (command chain). If we don't use the command chain, we usually call the previous object through the variable name, so we don't need to operate the stack. But with end(), we can chain all the method calls together:

$(&#39;ul.first&#39;).find(&#39;.foo&#39;).css(&#39;background-color&#39;, &#39;red&#39;)
  .end().find(&#39;.bar&#39;).css(&#39;background-color&#39;, &#39;green&#39;);

This chain of commands retrieves the items with class name foo in the first list and sets their background to red. end() will restore the object to the state before calling find(), so the second find() looks for '.bar' inside dca0a85f515854913184fc9b15104a03 rather than in the b50f8e6810c31b4d543db918bcbc30a7 and set the background of the matching element to green. The end result is that items 1 and 3 in the first list are set to a colored background, while the items in the second list have no change.

Recommended learning: "jquery video tutorial"

The above is the detailed content of What is the usage of end in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn