我有以下HTML代码,并希望删除<ul>
和<li>
代码,但保留内容。
<div class="test">
<ul>
<li> some text here</li>
</ul>
</div>
所以我希望结果是
<div class="test">
some text here
</div>
我可以用jQuery做到吗?
答案 0 :(得分:1)
您可以使用li
函数获取html
的内容作为标记,或只使用text
函数获取文本;然后只用那个标记/文本替换div
的内容:
// save text
text = $('.test li').html(); // or .text()
// remove the ul
$('.test ul').remove();
// append the text to the div.test
$('.test').append(text)
答案 1 :(得分:1)
尝试这样fiddle
$(document).ready(function(){
var elem=$(".test");
var content=elem.text();
elem.empty();
elem.text(content);
});
答案 2 :(得分:1)