2009-03-17 4 views

답변

134

당신은 empty 기능을 사용하려면 :

$('#mydiv').empty(); 
10

또 다른 방법은 빈 문자열로 HTML을 설정하는 단지입니다 : 내가 empty() 또는 html()는 생각하지 않습니다

$('#mydiv').html(''); 
69

무엇 를 찾고 있습니다. PHP에서 strip_tags과 같은 것을 찾고있는 것 같습니다. 이 작업을 수행하려면, 당신이 필요로하는 것보다이 기능을 추가 :

jQuery.fn.stripTags = function() { 
    return this.replaceWith(this.html().replace(/<\/?[^>]+>/gi, '')); 
}; 

을 가정이 당신의 HTML입니다 :

<div id='foo'>This is <b>bold</b> and this is <i>italic</i>.</div> 

그리고 당신은 수행

$("#foo").stripTags(); 

됩니다 어떤 입력 :

<div id='foo'>This is bold and this is italic.</div> 
+5

등) (분리 할 수 ​​있습니다 일시적이

<div class="prtDiv"> <div class="first"> </div> </div> 

같은 것이

$('.first').empty(); 

결과를 사용 모든 콘텐츠를 삭제합니다. 미래 세대를위한 좋은 대답은 여전히 ​​+1입니다. –

+28

개인적으로는 $ ("# foo") .html ($ ("# foo"). text()); – Killroy

+5

포스터가이 작업을 원했던 것은 아니지만 수행했습니다. +1. 고마워. –

0
var htmlJ = $('<p><span>Test123</span><br /><a href="http://www.google.com">goto Google</a></p>'); 
console.log(htmlJ.text()); // "Test123goto Google" 
+0

텍스트가''경고하므로 안전하지 않습니다. – umpirsky

0
function stripsTags(text) 
    { 
    return $.trim($('<div>').html(text).text()); 
    } 
+0

텍스트가''경고하므로 안전하지 않습니다. – umpirsky

0

515,는 "최초의"사업부 아래에있는 모든 HTML을 영구적으로 제거 할 경우이

<div class="prtDiv"> 
    <div class="first"> 
    <div class="hello">Hello</div> 
    <div class="goodbye">Goodbye</div> 
    </div> 
</div> 

같은 HTML이 있다고 가정합니다. (당신이 다시 추가 할 경우) 그냥 그가 의미 생각하는 우리가 시도

$('.first').detach(); 
관련 문제