programing

jQuery의 replaceWith()와 html()의 차이점은 무엇입니까?

sourcejob 2023. 8. 12. 10:14
반응형

jQuery의 replaceWith()와 html()의 차이점은 무엇입니까?

jQuery의 차이점은 무엇입니까?replaceWith()그리고.html()HTML이 매개 변수로 전달될 때 기능합니까?

다음 HTML 코드를 사용합니다.

<div id="mydiv">Hello World</div>

실행 중:

$('#mydiv').html('Aloha World');

결과:

<div id="mydiv">Aloha World</div>

실행 중:

$('#mydiv').replaceWith('Aloha World');

결과:

Aloha World

따라서 html()은 요소의 내용을 대체하고 replaceWith()는 실제 요소를 대체합니다.

replaceWith()는 현재 요소를 대체하는 반면 html()은 단순히 내용을 대체합니다.

replaceWith()는 실제로 요소를 삭제하지 않고 DOM에서 제거한 후 컬렉션에 있는 사용자에게 반환합니다.

피터의 예: http://jsbin.com/ofirip/2

html()과 replace() Jquery 함수를 사용하는 두 가지 방법이 있습니다.

<div id="test_id">
   <p>My Content</p>
</div>

1.) html() vs replaceWith()

var html = $('#test_id p').html();"내 콘텐츠"를 반환합니다.

그런데 그.var replaceWith = $('#test_id p').replaceWith();의 전체 DOM 개체를 반환합니다.<p>My Content</p>.


2.) html('value') vs replaceWith('value')

$('#test_id p').html('<h1>H1 content</h1>');다음과 같은 출력을 제공합니다.

<div id="test_id">
  <p><h1>H1 content</h1></p>
</div>

그런데 그.$('#test_id p').replaceWith('<h1>H1 content</h1>');다음과 같은 출력을 제공합니다.

<div id="test_id">
      <h1>H1 content</h1>
</div>

오래된 질문이지만 이것은 누군가에게 도움이 될 수 있습니다.

HTML이 올바르지 않은 경우 Internet Explorer 및 Chrome / Firefox에서 이러한 기능이 작동하는 방식에는 몇 가지 차이가 있습니다.

HTML을 정리하면 문서화된 대로 작동합니다.

(닫지 않음)</center>내 저녁 시간을 허비했어요!)

또한 그것을 아는 것도 유용할 수 있습니다..empty().append()대신 사용할 수도 있습니다..html()아래 표시된 벤치마크에서는 이 기능을 여러 번 호출해야 하는 경우에만 이 기능이 더 빠릅니다.

참조: https://jsperf.com/jquery-html-vs-empty-append-test

언급URL : https://stackoverflow.com/questions/730916/whats-the-difference-between-jquerys-replacewith-and-html

반응형