What is the usage of end in jquery

藏色散人
Release: 2021-11-12 10:17:47
Original
2178 people have browsed it

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()
Copy after login

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>
Copy after login

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;);
Copy after login

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

    rather than in the < li class="foo"> 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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!