在jQuery中,我想删除div中的所有HTML

在jQuery中,我想删除div中的所有HTML,jquery,Jquery,我有一个div,我想删除该div中的所有HTML 如何执行此操作?您想使用此功能: $('#mydiv').empty(); jQuery.fn.stripTags = function() { return this.replaceWith( this.html().replace(/<\/?[^>]+>/gi, '') ); }; 另一种方法是将html设置为空字符串: $('#mydiv').html(''); 我不认为empty()或html()是您要找的

我有一个div,我想删除该div中的所有HTML

如何执行此操作?

您想使用此功能:

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

另一种方法是将html设置为空字符串:

$('#mydiv').html('');
我不认为
empty()
html()
是您要找的。我猜您正在寻找类似PHP中的
strip\u标记
。如果要执行此操作,则需要添加此函数:

$('#mydiv').empty();
jQuery.fn.stripTags = function() {
    return this.replaceWith( this.html().replace(/<\/?[^>]+>/gi, '') );
};
这将导致:

<div id='foo'>This is bold and this is italic.</div>
这是粗体,这是斜体。
var htmlJ=$('Test123

); console.log(htmlJ.text());//“Test123转到谷歌”
功能条(文本)
{
返回$.trim($('').html(text.text());
}

假设您有这样一个html

<div class="prtDiv">
   <div class="first">
     <div class="hello">Hello</div>
     <div class="goodbye">Goodbye</div>
  </div>
</div>
结果是这样的

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

  </div>
</div>

我认为他的意思是删除所有内容。仍然是+1,为后代提供了一个很好的答案。就我个人而言,我只需要做$(“#foo”).html($(“#foo”).text());他并不是说他是否想要纯文本结果,但这个答案对我来说仍然有效:)很好,有一些小的改动,它对输入字段也很有效。非常感谢。如果文本为
alert('sasa')它将发出警报,因此不安全。如果文本为
警报('sasa')它将发出警报,因此不安全。
<div class="prtDiv">
   <div class="first">
     <div class="hello">Hello</div>
     <div class="goodbye">Goodbye</div>
  </div>
</div>
$('.first').empty();
<div class="prtDiv">
   <div class="first">

  </div>
</div>
$('.first').detach();