제이쿼리 .before(content [,content])
제이쿼리 .before(content [,content])
설명 : 일치하는 요소 세트의 각 요소 앞에 매개 변수로 지정된 컨텐츠를 삽입하십시오.
.before()및 .insertBefore()방법은 동일한 작업을 수행합니다.
주요 차이점은 구문과 특히 콘텐츠 및 대상의 배치에 있습니다.
With를 사용하면 .before()삽입 할 내용은 메서드의 인수에서 가져옵니다
$(target).before(contentToBeInserted). 로 .insertBefore(),
다른 한편으로는, 내용이 방법을 선행하고 차례로으로 전달되는 대상,
전에 삽입
.insertBefore()방법의 주장 : $(contentToBeInserted).insertBefore(target).
다음 HTML을 고려하십시오.
<div class="container">
<h2>Greetings</h2>
<div class="inner">Hello</div>
<div class="inner">Goodbye</div>
</div>
한 번에 여러 요소 앞에 내용을 작성하고 삽입 할 수 있습니다.
$( ".inner" ).before( "<p>Test</p>" );
각 내부 <div>요소는이 새로운 내용을 얻습니다.
<div class="container">
<h2>Greetings</h2>
<p>Test</p>
<div class="inner">Hello</div>
<p>Test</p>
<div class="inner">Goodbye</div>
</div>
페이지에서 요소를 선택하여 다른 요소 앞에 삽입 할 수도 있습니다.
$( ".container" ).before( $( "h2" ) );
이 방법으로 선택한 요소가 DOM의 다른 위치의 단일 위치에 삽입되면 대상 앞에 복제본이 이동합니다.
<h2>Greetings</h2>
<div class="container">
<div class="inner">Hello</div>
<div class="inner">Goodbye</div>
</div>
중요 : 그러나 하나 이상의 대상 요소가있는 경우 마지막 요소를 제외한 각 대상에 대해 삽입 된 요소의 복제본이 만들어집니다.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>before demo</title>
<style>
p {
background: yellow;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<p> is what I said...</p>
<script>
$( "p" ).before( "<b>Hello</b>" );
</script>
</body>
</html>
게시판 목록
개발자팁
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4616 | PHP | 8년 전 | 2514 | ||
| 4615 | PHP | 8년 전 | 1708 | ||
| 4614 | jQuery |
|
8년 전 | 2747 | |
| 4613 | MySQL |
|
8년 전 | 3679 | |
| 4612 | jQuery | 8년 전 | 2669 | ||
| 4611 | JavaScript | 8년 전 | 2324 | ||
| 4610 | PHP | 8년 전 | 1998 | ||
| 4609 | PHP | 8년 전 | 1724 | ||
| 4608 | PHP | 8년 전 | 2234 | ||
| 4607 | PHP | 8년 전 | 2848 | ||
| 4606 | PHP | 8년 전 | 2201 | ||
| 4605 | PHP | 8년 전 | 2109 | ||
| 4604 | node.js |
대동소프트
|
8년 전 | 1685 | |
| 4603 | node.js |
대동소프트
|
8년 전 | 2132 | |
| 4602 | node.js |
대동소프트
|
8년 전 | 1840 | |
| 4601 | PHP | 8년 전 | 2348 | ||
| 4600 | PHP | 8년 전 | 2491 | ||
| 4599 | PHP | 8년 전 | 2331 | ||
| 4598 | PHP | 8년 전 | 2356 | ||
| 4597 | PHP | 8년 전 | 2546 | ||
| 4596 | PHP | 8년 전 | 1913 | ||
| 4595 | PHP | 8년 전 | 2398 | ||
| 4594 | PHP | 8년 전 | 2158 | ||
| 4593 | PHP | 8년 전 | 2222 | ||
| 4592 | PHP | 8년 전 | 2278 | ||
| 4591 | PHP | 8년 전 | 2615 | ||
| 4590 | PHP | 8년 전 | 2216 | ||
| 4589 | node.js |
|
8년 전 | 2009 | |
| 4588 | jQuery |
프로그래머7
|
8년 전 | 1678 | |
| 4587 | jQuery |
프로그래머7
|
8년 전 | 1751 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기