코드 스 니펫 :Jquery html 속성 : 요소의 html을 요소의 html에서 제외하는 방법
<div id="some_text">
<p>some text</p>
<div id="child">
<p>some other text</p>
</div>
</div
어떻게 "<p>some text</p>
"만 얻을 수 있습니까?
코드 스 니펫 :Jquery html 속성 : 요소의 html을 요소의 html에서 제외하는 방법
<div id="some_text">
<p>some text</p>
<div id="child">
<p>some other text</p>
</div>
</div
어떻게 "<p>some text</p>
"만 얻을 수 있습니까?
$('#some_text:first-child').html(); //contains your text
$('#some_text:first-child').remove(); //to remove
사용 replaceWith()
:
$("#some_text > p").replaceWith("some other html");
또는 좀 더 구체적으로해야하는 경우 :
$("#some_text").children().not("#child").replaceWith("some other html");
replaceWith()
의지 담당자 : 심지어
$("#some_text > p:first-child").replaceWith("some other html");
또는 not()
를 사용 레이스 각은 특정 HTML과 일치하는 요소이므로 두 개 이상 일치하면 중복 된 콘텐츠가 표시됩니다. 다른 방법은 HTML을 제거한 다음 원하는 HTML을 추가하는 것입니다.
기본적으로 새 HTML을 넣기 때문에 하위 요소를 유지하면서 div 내에서 HTML을 바꾸는 것은 어렵습니다. 그 전에? 후? 약? 아마도 좀 더 구체적 일 필요가있을 것입니다.
$('#sometext > p')
트릭을해야합니다.
0x3이 (가) :nth
자식에 대해 :first-child
:parent
과 같은 의사 선택기를 사용하지 않으려 고합니다.