日期:2014-05-16  浏览次数:20370 次

问jquery live函数的js表现形式。。
HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
    <div id="tip1" style="width:100px;height:100px;border:1px solid red">
          <div  id="sp" style="height:50px;height:50px;border:1px solid blue"></div>
    </div>
    <div id="tip2" style="width:100px;height:100px;border:1px solid green"></div>
<script>
document.getElementById("tip1").onclick=function(){    
    document.getElementById("tip2").innerHTML=document.getElementById("tip1").innerHTML;
    document.getElementById("tip1").innerHTML='';
}
document.getElementById("sp").onclick=function(e){
    var e=e||window.event;
     if (!document.all)  
            e.stopPropagation()  
        else 
            window.event.cancelBubble=true 
    alert(111);
}
</script>
</body>
</html>


看例子,首先点击tip1,
然后sp区域到tip2的内容里,
这时sp点击事件失效了,

问如何使sp产生效果


------解决方案--------------------
如果是想保留sp的点击事件,原生的DOM树操作方法:
HTML code
<head>
    <title></title>
</head>
<body>
    <div id="tip1" style="width:100px;height:100px;border:1px solid red">
        <div id="sp" style="height:50px;height:50px;border:1px solid blue"></div>
    </div>
    <div id="tip2" style="width:100px;height:100px;border:1px solid green"></div>
    <script type="text/javascript">
        var sp = document.getElementById("sp");
        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(111);
        };
        tip1.onclick = function () {
            this.removeChild(sp);
            tip2.appendChild(sp);
        };
    </script>
</body>