jQuery / Reference / .insertAfter()
개요
.insertAfter()는 특정 요소 뒤에 요소를 추가하거나 이동시킵니다.
문법
.insertAfter( target )
.insertAfter( target )
.insertAfter( target )
예제 1
- h1 요소 뒤에 <p>World</p>를 추가합니다.
<!doctype html>
<html lang="ko">
<head>
<meta charset="utf-8">
<title>jQuery</title>
<style>
body {
font-family: Consolas, monospace;
font-size: 16px;
}
</style>
<script src="//code.jquery.com/jquery-3.7.1.js"></script>
<script>
$( document ).ready( function() {
$( '<p>World</p>' ).insertAfter( 'h1' );
} );
</script>
</head>
<body>
<h1>Hello</h1>
</body>
</html>
<!doctype html>
<html lang="ko">
<head>
<meta charset="utf-8">
<title>jQuery</title>
<style>
body {
font-family: Consolas, monospace;
font-size: 16px;
}
</style>
<script src="//code.jquery.com/jquery-3.7.1.js"></script>
<script>
$( document ).ready( function() {
$( '<p>World</p>' ).insertAfter( 'h1' );
} );
</script>
</head>
<body>
<h1>Hello</h1>
</body>
</html>
<!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>jQuery</title> <style> body { font-family: Consolas, monospace; font-size: 16px; } </style> <script src="//code.jquery.com/jquery-3.7.1.js"></script> <script> $( document ).ready( function() { $( '<p>World</p>' ).insertAfter( 'h1' ); } ); </script> </head> <body> <h1>Hello</h1> </body> </html>
예제 2
- h1 요소 뒤로 p 요소를 이동시킵니다.
<!doctype html>
<html lang="ko">
<head>
<meta charset="utf-8">
<title>jQuery</title>
<style>
body {
font-family: Consolas, monospace;
font-size: 16px;
}
</style>
<script src="//code.jquery.com/jquery-3.7.1.js"></script>
<script>
$( document ).ready( function() {
$( 'p' ).insertAfter( 'h1' );
} );
</script>
</head>
<body>
<p>World</p>
<h1>Hello</h1>
</body>
</html>
<!doctype html>
<html lang="ko">
<head>
<meta charset="utf-8">
<title>jQuery</title>
<style>
body {
font-family: Consolas, monospace;
font-size: 16px;
}
</style>
<script src="//code.jquery.com/jquery-3.7.1.js"></script>
<script>
$( document ).ready( function() {
$( 'p' ).insertAfter( 'h1' );
} );
</script>
</head>
<body>
<p>World</p>
<h1>Hello</h1>
</body>
</html>
<!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>jQuery</title> <style> body { font-family: Consolas, monospace; font-size: 16px; } </style> <script src="//code.jquery.com/jquery-3.7.1.js"></script> <script> $( document ).ready( function() { $( 'p' ).insertAfter( 'h1' ); } ); </script> </head> <body> <p>World</p> <h1>Hello</h1> </body> </html>
참고
- A.after( B ) : A 뒤에 B를 추가합니다.
- A.insertAfter( B ) : B 뒤에 A를 추가합니다.
- A.before( B ) : A 앞에 B를 추가합니다.
- A.insertBefore( B ) : B 앞에 A를 추가합니다.