【发布时间】:2011-09-24 10:58:35
【问题描述】:
我需要将 li 元素从其父 ul 中移出并移到主父 ul 中。
我试过了:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script type="text/javascript">
$( init );
function init() {
// Move
$('ul#listcomments').append( $('.children>li') );
// Delete
$('.children').remove();
}
</script>
</head>
<body>
<ul id="listcomments">
<li>Comment 1</li>
<li>Comment 2</li>
<li>Comment 3</li>
<li>Comment 4</li>
<li>Comment 5 <ul class="children"><li>Child comment of #5 is this</li></ul></li>
<li>Comment 6</li>
<li>Comment 7</li>
<li>Comment 8</li>
</ul>
</body>
</html>
我想实现这个: * 即让子 li#5child 脱离 ul 和 li 出现在 li 的正下方。
<ul id="listcomments">
<li>Comment 1</li>
<li>Comment 2</li>
<li>Comment 3</li>
<li>Comment 4</li>
<li>Comment 5</li>
<li>Child comment of #5 is this</li>
<li>Comment 6</li>
<li>Comment 7</li>
<li>Comment 8</li>
</ul>
相反,我得到: * append 将 li 放在外面,但放在 ul#listcmets 的最后
<ul id="listcomments">
<li>Comment 1</li>
<li>Comment 2</li>
<li>Comment 3</li>
<li>Comment 4</li>
<li>Comment 5 </li>
<li>Comment 6</li>
<li>Comment 7</li>
<li>Comment 8</li>
<li>Child comment of #5 is this</li>
</ul>
我希望这比使用文字更好地解释它。这个概念很简单,但由于我的技能有限,我想不出更好的办法。
帮助! :)
*注意:lis 的命名和编号是任意的。并且子评论可以出现在任何#li 中。内容是动态生成的。
【问题讨论】: