Home >Web Front-end >JS Tutorial >The difference between append, prepend, before and after methods in jquery
<p class='a'> //<---you want p c to append in this <p class='b'>b</p> </p>
is used
$('.a').append($('.c'));
The effect is as follows:
<p class='a'> //<---you want p c to append in this <p class='b'>b</p> <p class='c'>c</p> </p>
Similarly use
$('.a').prepend($('.c'));
The effect is as follows:
<p class='a'> //<---you want p c to append in this <p class='c'>c</p> <p class='b'>b</p> </p>
Also use the hypothetical code:
$('.a').after($('.c'));
The effect is as follows:
<p class='a'> <p class='b'>b</p></p><p class='c'>c</p>
Also use before()
$('.a').before($('.c'));
The effect is as follows:
<p class='c'>c</p><p class='a'> <p class='b'>b</p></p>
<p class='a'> //<---you want p c to append in this <p class='b'>b</p> </p>
$('.a').append($('.c'));The effect is as follows:
<p class='a'> //<---you want p c to append in this <p class='b'>b</p> <p class='c'>c</p> </p>Similarly use
$('.a').prepend($('.c'));The effect is as follows:
<p class='a'> //<---you want p c to append in this <p class='c'>c</p> <p class='b'>b</p> </p>2. Use after () and before() also use the hypothetical code:
$('.a').after($('.c'));The effect is as follows:
<p class='a'> <p class='b'>b</p></p><p class='c'>c</p>Also use before()
$('.a').before($('.c'));The effect is as follows:
<p class='c'>c</p><p class='a'> <p class='b'>b</p></p>Summary: append() & prepend() is to insert content within the element (the content becomes a child element or node of the element), after() & before() is to Insert content outside the element (its content becomes the sibling node of the element)
The above is the detailed content of The difference between append, prepend, before and after methods in jquery. For more information, please follow other related articles on the PHP Chinese website!