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

如何取消事件冒泡?
我要的是只要光标还在wrap上就不触发onmouseout.
现在是光标移到a链接上都触发onmouseout.
HTML code
<!doctype HTML>
<html>
    <head>
<title>scroll</title>
        <style type="text/css">
            #box{width:400px;height:143px;overflow:hidden;position:relative;float:left}
            ul{position:absolute;left:0;top:0;margin:0px}
            #slider{width:10px;height:143px;background:#ccc;float:left;position:relative}
            #slider_btn{position:absolute;top:0;left:0;height:30px;width:10px;background:orange}
            #wrap{background:orange;padding:10px 0}
            #wrap a{background:red}
            #box{background:blue}
        </style>

    </head>


    <body>

        <div id="wrap">
            <a href="">sdsad</a>
        </div>
    <div id="box">sdsd</div>
        <script type="text/javascript">
            var $ = function(id){return document.getElementById(id)};
            function aa(){
                $("box").style.display = "block";
                console.log("aa")
            }
            function bb(){
                $("box").style.display = "none";
                console.log("bb")
            }
            $("wrap").onmouseover = function(e){
                    aa();
            }
            $("wrap").onmouseout = function(e){
                    bb();
            }
        </script>
    </body>
</html>


------解决方案--------------------
HTML code
<!doctype HTML>
<html>
    <head>
        <title>scroll</title>
        <style type="text/css">
            #box{width:400px;height:143px;overflow:hidden;position:relative;float:left;display:none}
            ul{position:absolute;left:0;top:0;margin:0px}
            #slider{width:10px;height:143px;background:#ccc;float:left;position:relative}
            #slider_btn{position:absolute;top:0;left:0;height:30px;width:10px;background:orange}
            #wrap{background:orange;padding:10px 0}
            #wrap a{background:red}
            #box{background:blue}
        </style>
    </head>
    <body>
        <div id="wrap">
            <a href="">sdsad</a>
        </div>
        <div id="box">sdsd</div>
        <script type="text/javascript">
            var $ = function(id){
                return document.getElementById(id)
            };
            function aa(){
                $("box").style.display = "block";
                console.log("aa")
            }
            function bb(){
                $("box").style.display = "none";
                console.log("bb")
            }
            function hasChild(elem1, elem2){
                var childs = elem1.getElementsByTagName('*');
                for(var i = 0, len = childs.length; i < len; i++){
                    if(elem2 === childs[i]){
                        return true;
                    }
                }
                return false;
            }
            $("wrap").onmouseover = function(e){
                var event = e || window.event,
                    target = event.target || event.srcElement,
                    relatedTarget = event.relatedTarget || event.fromElement;
                if(target === this && !hasChild(this,relatedTarget)){
                    aa();
                }
            }
            $("wrap").onmouseout = function(e){
                var event = e || window.event,