jQuery / Reference / .prepend()

개요

  • .prepend()는 선택한 요소의 내용의 앞에 콘텐트를 추가합니다.

문법

.prepend( content [, content ] )

예제 1

  • 순서 없는 목록 처음에 Dolor를 추가합니다.
<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>jQuery</title>
    <style>
      body {
        line-height: 2;
        font-family: sans-serif;
        font-size: 20px;
      }
    </style>
    <script src="//code.jquery.com/jquery-3.3.1.min.js"></script>
    <script>
      $( document ).ready( function() {
        $( 'ul' ).prepend( '<li>Dolor</li>' );
      } );
    </script>
  </head>
  <body>
    <ul>
      <li>Lorem</li>
      <li>Ipsum</li>
    </ul>
  </body>
</html>

예제 2

  • strong 요소를 p 요소의 내용의 앞으로 이동시킵니다.
<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>jQuery</title>
    <style>
      body {
        line-height: 2;
        font-family: sans-serif;
        font-size: 20px;
      }
    </style>
    <script src="//code.jquery.com/jquery-3.3.1.min.js"></script>
    <script>
      $( document ).ready( function() {
        $( 'p' ).prepend( $( 'strong' ) );
      } );
    </script>
  </head>
  <body>
    <p>abc</p>
    <strong>XYZ</strong>
  </body>
</html>

같은 카테고리 다른 글
jQuery / Reference / .each()

jQuery / Reference / .each()

jQuery의 .each() 함수는 주로 배열, 객체, 또는 jQuery 선택자로 선택된 요소 집합에 대해 반복 작업을 수행하기 위해 사용됩니다. JavaScript의 for 또는 forEach 루프와 유사하지만, jQuery의 특성과 통합된 방식으로 작동합니다.

jQuery / Reference / :contains()

jQuery / Reference / :contains()

:contains()는 특정 문자열을 포함한 요소를 선택하는 선택자입니다. 문자열 포함 여부를 따질 때 대소문자를 구분한다는 점에 주의합니다.

jQuery / Reference / :button

jQuery / Reference / :button

:button은 type이 button인 요소를 선택하는 선택자입니다.

jQuery / Reference / .width()

jQuery / Reference / .width()

.width()는 선택한 요소의 가로 크기를 반환하거나, 가로 크기를 변경합니다.

jQuery / Reference / .val()

jQuery / Reference / .val()

개요 .val()은 양식(form)의 값을 가져오거나 값을 설정합니다. 문법 1 .val() 선택한 양식의 값을 가져옵니다. 예를 들어 다음은 아이디가 jbInput인 input 요소의 값을 변수 jb에 저장합니다. var jb = $( 'input#jbInput' ).val(); 문법 2 .val( value ) 선택한 양식의 값을 설정합니다. 예를 들어 다음은 아이디가 jbInput인 input 요소의 값을 ABCDE로 정합니다. $( 'input#jbInput' ).val( 'ABCDE' ); 예제 1 양식에 텍스트를 입력하고 버튼을 클릭하면, ...