.html()

· 2017-10-20 (금) 00:55:46 · 2202

.html()

설명 : 일치하는 요소 세트에서 첫 번째 요소의 HTML 내용을 가져옵니다.

이 방법은 XML 문서에서는 사용할 수 없습니다.

HTML 문서에서 .html()모든 요소의 내용을 가져 오는 데 사용할 수 있습니다. 선택기 표현식이 둘 이상의 요소와 일치하면 첫 번째 일치 항목 만 HTML 내용을 반환합니다. 다음 코드를 고려하십시오.

$( "div.demo-container" ).html();

다음 <div>내용을 검색 class="demo-container"하려면 문서에있는 첫 번째 내용이어야합니다 .
<div class="demo-container">
  <div class="demo-box">Demonstration Box</div>
</div>
결과는 다음과 같습니다.
<div class="demo-box">Demonstration Box</div>
이 메서드는 브라우저의 innerHTML속성을 사용합니다 . 일부 브라우저는 원본 문서의 HTML 소스를 정확하게 복제하는 HTML을 반환하지 않을 수 있습니다. 예를 들어 Internet Explorer는 영숫자 문자 만 포함하는 경우 특성 값 주변의 따옴표를 닫는 경우가 있습니다.

추가 참고 사항 :

설계함으로써, HTML 문자열을 받아들이는 임의의 jQuery 생성자 또는 방법 - jQuery를 ()는 , 으로 .Append () , 후론 () 등 - 잠재적 코드를 실행할 수있다. 이는 스크립트 태그를 삽입하거나 코드를 실행하는 HTML 속성을 사용하여 발생할 수 있습니다 (예 :) <img onload="">. URL 쿼리 매개 변수, 쿠키 또는 양식 입력과 같이 신뢰할 수없는 출처에서 가져온 문자열을 삽입 할 때이 메서드를 사용하지 마십시오. 그렇게하면 XSS (Cross-Site-Scripting) 취약점이 발생할 수 있습니다. 문서에 내용을 추가하기 전에 사용자 입력을 제거하거나 이스케이프 처리하십시오.
예:
단락을 클릭하여 HTML에서 텍스트로 변환하십시오.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>html demo</title>
  <style>
  p {
    margin: 8px;
    font-size: 20px;
    color: blue;
    cursor: pointer;
  }
  b {
    text-decoration: underline;
  }
  button {
    cursor: pointer;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
 
<p>
  <b>Click</b> to change the <span id="tag">html</span>
</p>
<p>
  to a <span id="text">text</span> node.
</p>
<p>
  This <button name="nada">button</button> does nothing.
</p>
 
<script>
$( "p" ).click(function() {
  var htmlString = $( this ).html();
  $( this ).text( htmlString );
});
</script>
 
</body>
</html>

|
댓글을 작성하시려면 로그인이 필요합니다.

개발자팁

5,403건

개발과 관련된 유용한 정보를 공유하세요. 질문은 QA에서 해주시기 바랍니다.

+
분류 제목 글쓴이 날짜 조회
jQuery 17-11-24 조회 2,535
jQuery 17-11-24 조회 2,692
jQuery 17-11-21 조회 2,917
jQuery 17-11-21 조회 3,129
jQuery 17-11-21 조회 2,342
jQuery 17-11-10 조회 3,340
jQuery 17-11-10 조회 2,803
jQuery
[jQuery]
17-11-10 조회 2,151
JavaScript 17-11-09 조회 3,237
jQuery
[jQuery]
17-11-08 조회 1,961
jQuery 17-11-08 조회 2,230
jQuery 17-11-08 조회 2,331
jQuery 17-10-24 조회 2,109
jQuery 17-10-24 조회 2,184
jQuery
[jQuery]
17-10-24 조회 1,868
jQuery 17-10-20 조회 2,434
jQuery 17-10-20 조회 2,913
jQuery
[jQuery]
17-10-20 조회 2,203
jQuery 17-10-19 조회 1,894
jQuery
[jQuery]
17-10-19 조회 2,326
jQuery 17-10-19 조회 5,920
기타 17-10-19 조회 3,657
jQuery
[jQuery]
17-10-17 조회 2,049
jQuery 17-10-17 조회 2,207
jQuery 17-10-17 조회 2,298
PHP 17-10-16 조회 5,610
node.js 17-10-13 조회 4,646
jQuery 17-10-12 조회 2,695
jQuery 17-10-12 조회 2,138
jQuery 17-10-12 조회 1,672