猿问

使用js操作dom?

如何使下边这个有序列表变成另一个,也就是将4移入1的下边

<ul>        <li id="1">1</li>
        <li id="2">2</li>
        <li id="3">3</li>        <li id="4">4</li>
    </ul>

 <ul>        <li id="1">1</li>
        <li id="4">4</li>
        <li id="2">2</li>        <li id="3">3</li>
    </ul>


隔江千里
浏览 524回答 2
2回答

一只萌萌小番薯

思路最简使用appendChild()方法,利用其将元素移除DOM树后再重新加入。<ul>&nbsp; &nbsp; <li id="1">1</li>&nbsp; &nbsp; <li id="2">2</li>&nbsp; &nbsp; <li id="3">3</li>&nbsp; &nbsp; <li id="4">4</li></ul><button id="implement">执行</button><!--创建一个按钮以便演示效果--><script>&nbsp; &nbsp; var el = document.getElementById('4');&nbsp; &nbsp; //获取要操作的元素&nbsp; &nbsp; &nbsp; &nbsp; list = document.getElementsByTagName("li");&nbsp; &nbsp; &nbsp; &nbsp; //获取要操作的列表序列&nbsp; &nbsp; &nbsp; &nbsp; implement = document.getElementById('implement');&nbsp; &nbsp; &nbsp; &nbsp; //获取执行按钮&nbsp; &nbsp; implement.onclick = function () {&nbsp; &nbsp; &nbsp; &nbsp; list[0].appendChild(el);&nbsp; &nbsp; &nbsp; &nbsp; //list[0]的后边加入元素el;&nbsp; &nbsp; }</script>
随时随地看视频慕课网APP

相关分类

JQuery
我要回答