删除<ul>和<li>但保留内容</li> </ul>

时间:2015-03-17 11:43:11

标签: jquery

我有以下HTML代码,并希望删除<ul><li>代码,但保留内容。

<div class="test">
  <ul>
    <li> some text here</li>
  </ul>
</div>

所以我希望结果是

<div class="test">
   some text here
</div>

我可以用jQuery做到吗?

3 个答案:

答案 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)

可以使用html(function)方法:

$(".test").html(function(){
    return $(this).find('li').html();
});

DEMO