<!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产生效果

解决方案 »

  1.   

    如果是想保留sp的点击事件,原生的DOM树操作方法:
    <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>使用操作jQuery对象引用的方法:
    <head>
        <title></title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                $('#sp').click(function (e) {
                    var e = e || window.event;
                    if (!document.all) e.stopPropagation()
                    else window.event.cancelBubble = true
                    alert(111);
                });
                $('#tip1').click(function () {
                    $(this).children().prependTo($('#tip2'));
                });
            });
        </script>
    </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>
    </body>如果使用live绑定,根据id判断,事情会变得非常复杂,一方面tip1要把自己的内容赋给tip2,还要清空自己,很快会发现产生非常多的副效果,冒泡什么的处理也麻烦起来了。
    <head>
        <title></title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                $('#sp').live('click', function (e) {
                    //...
                    alert(111);
                });
                $('#tip1').click(function () {
                    $('#tip2').html(this.innerHTML);
                    $(this).html('');
                });
            });
        </script>
    </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>
    </body>