jQuery 새 요소 생성

Jmnote bot (토론 | 기여)님의 2020년 11월 2일 (월) 02:53 판 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

jQuery 새 요소 생성

1 예시 1: div 생성[ | ]

<script src="//code.jquery.com/jquery.min.js"></script>
<script>
  $(function() {
    $('#button-add-div').click(function() {
      $("<div>", {
        css: {
          width: "200px",
          height: "50px",
          backgroundColor: "skyblue",
          border: "1px solid royalblue"
        },
      }).appendTo("body");
    });
  });
</script>

<button id='button-add-div'>추가</button>

2 예시 2: text, click 포함[ | ]

  • 위 예시에 text와 click이벤트를 더함
<script src="//code.jquery.com/jquery.min.js"></script>
<script>
  $(function() {
    $('#button-add-div').click(function() {
      $("<div>", {
        css: {
          width: "200px",
          height: "30px",
          backgroundColor: "yellow",
          border: "1px solid royalblue"
        },
        text: "안녕하세요 (클릭가능)",
        click: function() {
          alert('클릭됨');
        }
      }).appendTo("body");
    });
  });
</script>

<button id='button-add-div'>추가</button>

3 같이 보기[ | ]

4 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}