我正在尝试从 2 个 div 元素中删除内容而不删除 div 本身。
<div id='test'>
content I want to remove
<div id='childDiv'>
content I want to remove
</div>
</div>
我想保留 childDiv 和测试 div 但删除它们的内容。我怎么做?
$('#test').empty()
也会删除childDiv
。
我正在尝试从 2 个 div 元素中删除内容而不删除 div 本身。
<div id='test'>
content I want to remove
<div id='childDiv'>
content I want to remove
</div>
</div>
我想保留 childDiv 和测试 div 但删除它们的内容。我怎么做?
$('#test').empty()
也会删除childDiv
。
如何存储 childDiv 然后清空父项,然后将内部子项放回去?
$('#test').html($('#childDiv').html()) // didn't quite work
// this clears both divs and leaves only the structure
$('#test').html($('#childDiv').html(""));
jsFiddle,用 chrome 检查生成的黑线,检查。http://jsfiddle.net/tbspn/
这是 JavaScript 的方法:
document.getElementById('test').innerHTML = '';
document.getElementById('childDiv').innerHTML = '';
$('#test').html('');
也清除所有内部标记。
这应该工作:
$('#test').text('');
$('#childDiv').text('');