var tmp=$("tip1").innerHTML; $("tip1").innerHTML=$("tip2").innerHTML; $("tip2").innerHTML=tmp;
这种方法没错,但是如果sp,sp2节点上注册了事件,按第一种方法处理,事件会失效。
方法2
<script type="text/javascript"> var sp = document.getElementById("sp"); var sp2 = document.getElementById("sp2"); var tip1 = document.getElementById("tip1"); var tip2 = document.getElementById("tip2"); sp.onclick = function (e) { var e = e || window.event; if (!document.all) e.stopPropagation() else window.event.cancelBubble = true alert('sp1'); }; sp2.onclick = function (e) { var e = e || window.event; if (!document.all) e.stopPropagation() else window.event.cancelBubble = true alert('sp2'); }; tip1.onclick = function () { tip2.appendChild(sp); tip1.appendChild(sp2); }; </script>