Home > Web Front-end > JS Tutorial > About the use of end() in jQuery_jquery

About the use of end() in jQuery_jquery

WBOY
Release: 2016-05-16 18:05:08
Original
1295 people have browsed it

The end() method is described in the official API like this: "Go back to before the most recent "destructive" operation. That is, change the matching element list to the previous state.";
It seems like Find the previous element of the last operated element, in the following example:
html code:

Copy code code As follows:

Test content 1

Test content 2


jQuery code:
Copy code The code is as follows:

$('

New content


The result obtained is:
Copy code The code is as follows:

Test content 1

New Add content


Test content 2

Add content



I don’t quite understand here. Why only the first

tag has two styles? What is returned after the end() method? I added monitoring in Firefox and got the following results:
1.$('

New content

').appendTo('div') returns: [p,p] object array, that is, the two added p tags ;
2.$('

New content

').appendTo('div').addClass('c1') returns: [p.c1,p.c1] Object array, that is, the p object array after adding the c1 class style;
3.$('

New content

').appendTo('div').addClass('c1' ).end() returns [p.c1], which is the

in the first

. In the 2nd operation, the last "destroyed" is the < in the second
;p>, so the object of his previous operation was the

in the first

, and that is what is returned;
4.$('

New content').appendTo('div').addClass('c1').end().addClass('c2') still returns the

;
in the first

Now I understand a little bit, the key is to find out what is the previous element of the last operated element.
Related labels:
end
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