InsertBefore (): A.insertbefore (b)
A in front, b in the rear,
A: is a selector, B: also a selector
<!DOCTYPE HTML><HTML><Head> <MetaCharSet= ' UTF-8 '> <title>Jqu</title> <Scripttype= "Text/javascript"src= ' Jquery-2.2.0.min.js '></Script></Head><Body><Pclass= ' P1 '>P1:hello</P>Hello World<Pclass= ' P2 '>P2:wenwen</P>Hello wo</Body><Scripttype= "Text/javascript"> $(function(){ $('. P2'). InsertBefore ('. P1'); })</Script></HTML>
Get:
P2:wenwenp1:hellohello World Hello Wo
InsertAfter (): With InsertBefore () is the same truth
A.insertafter (b)
A after, b in front
Now it is said before ()
Before (): A.before ()
A is a selector already on the page, B is what you need to add, note: What it is, identify the tag, B is not a selector
A after, b in front
<!DOCTYPE HTML><HTML><Head> <MetaCharSet= ' UTF-8 '> <title>Jqu</title> <Scripttype= "Text/javascript"src= ' Jquery-2.2.0.min.js '></Script></Head><Body><Pclass= ' P1 '>P1:hello</P><Pclass= ' P2 '>P2:wenwen</P></Body><Scripttype= "Text/javascript"> $(function(){ $('. P2'). Before ('. P1'); })</Script></HTML>
Finally get:
P1:hello.p1p2:wenwen
See? p1 does not recognize the selector, directly the string, in the front position of the. P2 Selector
After (): With before () is the same, just one in the previous
I just want to say that the difference between insertbefore (), InsertAfter () and before (), after (), I feel that the biggest difference is to see the scene you want to use, if you need two selector position switch is used insertbefore (), InsertAfter ()
If you need a selector and a text to swap positions you can use before (), after (); Of course, this is not just a replacement position.
Swap position is that the page already exists, this method can also add things that are not on the page, such as:
$ (' <p class= ' p3 ' > Hehe </p>'). InsertBefore ('. P1');
jquery insertbefore (), InsertAfter (), after (), before ()