首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

怎么取消事件冒泡

2012-05-24 
如何取消事件冒泡?我要的是只要光标还在wrap上就不触发onmouseout.现在是光标移到a链接上都触发onmouseout

如何取消事件冒泡?
我要的是只要光标还在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,                    target = event.target || event.srcElement,                    relatedTarget = event.relatedTarget || event.toElement;                if(target === this && !hasChild(this,relatedTarget)){                    bb();                }              }        </script>    </body></html> 

热点排行
Bad Request.