猿问

关于函数的调用疑问

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style type="text/css">
    #div1{width:200px;height:30px;background-color:red;}
    #div2{width:150px; height:20px;background-color:gray;margin-top:10px;display:none;}
</style>
<script type="text/javascript">
    window.onload = function () {
        var div1 = document.getElementById("div1");
        var div2 = document.getElementById("div2");
        var timer = null;
        function show() {
            div2.style.display = "block";
            clearTimeout(timer);
        }
        function hide() {
            timer = setTimeout(function () { div2.style.display = "none"; }, 300);
        }
        div1.onmouseover = show;
        div1.onmouseout = hide;
        div2.onmouseover = show;
        div2.onmouseout = hide;
    }
</script>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
</body>
</html>
        div1.onmouseover = show;
        div1.onmouseout = hide;
        div2.onmouseover = show;
        div2.onmouseout = hide;为啥我要是写成div1.onmouseover = show();
                                              div1.onmouseout = hide();
                                              div2.onmouseover = show();
                                              div2.onmouseout = hide();就不行呢?函数的调用不得加上后面的括号吗?求解答


天窗的蚂蚁
浏览 1320回答 1
1回答

慕无忌8876879

加上括号,你的方法就执行了,但是不会赋值给你的div1.onmouseover了,当出发你的div1.onmouseover的时候就不会调用show了
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答