在jQuery中我想删除div中的所有HTML
我有一个div,我想删除该div内的所有HTML。
我怎样才能做到这一点?
你想使用空函数:
$('#mydiv').empty();
我不认为empty()
或html()
是你正在寻找的。 我猜你在PHP中正在寻找像strip_tags
这样的东西。 如果你想这样做,那么你需要添加这个function:
jQuery.fn.stripTags = function() { return this.replaceWith( this.html().replace(/<\/?[^>]+>/gi, '') ); };
假设这是你的HTML:
This is bold and this is italic.
然后你做:
$("#foo").stripTags();
这将导致:
This is bold and this is italic.
另一种方法是将html设置为空字符串:
$('#mydiv').html('');
var htmlJ = $('Test123
goto Google
'); console.log(htmlJ.text()); // "Test123goto Google"
function stripsTags(text) { return $.trim($('').html(text).text()); }
假设你有这样的HTML
Hello Goodbye
如果你想让“first”div下的所有html永久删除。 只是用这个
$('.first').empty();
结果将是这样的
对于临时(如果你想再次添加它们)我们可以尝试detach()之类的
$('.first').detach();