<html>
<head>
    <title>test</title>
    [code=JScript]
<script type="text/javascript">
        function doOver(divId) {
            document.getElementById(divId).style.backgroundColor = "Green";
        }
        function doOut(divId) {
            document.getElementById(divId).style.backgroundColor = "Blue";
        }
    </script></head>
<body>
    <div id="div1" onmouseover="doOver('div1');">
        one</div>
    <div id="div2" onmouseover="doOver('div2');">
        two</div>
    <div id="div3" onmouseover="doOver('div3');">
        three</div>
</body>
</html>[/code]
这样写三个div的onmouseover效果都可以实现,那为什么下面这样写就不可以呢?<html>
<head>
    <title>test</title>
[code=JScript]
    <script type="text/javascript">
        function doProcess() {
            document.getElementById('div1').onmouseover = doOver('div1');
            document.getElementById('div2').onmouseover = doOver('div2');
            document.getElementById('div3').onmouseover = doOver('div3');
        }
        function doOver(divId) {
            document.getElementById(divId).style.backgroundColor = "Green";
        }
        function doOut(divId) {
            document.getElementById(divId).style.backgroundColor = "Blue";
        }
    </script></head>
<body onload="doProcess();">
    <div id="div1">
        one</div>
    <div id="div2">
        two</div>
    <div id="div3">
        three</div>
</body>
</html>[/code]

解决方案 »

  1.   

    1、绑定事件的脚本执行在元素创建之前;
    2、doOver返回的不是函数。
      

  2.   

    1、在onload下执行不是应该是元素已经创建了吗?
    2、返回的不是函数,什么意思?我不明白!
      

  3.   


    <!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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title><script language="javascript">
     function doProcess() {
                document.getElementById('div1').onmouseover = doOver;
                document.getElementById('div2').onmouseover = doOver;
                document.getElementById('div3').onmouseover = doOver;
                document.getElementById('div1').onmouseout = doOut;
                document.getElementById('div2').onmouseout = doOut;
                document.getElementById('div3').onmouseout = doOut;
            }
            function doOver() {
                this.style.backgroundColor = "Green";
            }
            function doOut() {
                this.style.backgroundColor = "white";
            }</script>
    </head><body onload="doProcess();">
      <div id="div1">
      one</div>
      <div id="div2">
      two</div>
      <div id="div3">
      three</div>
    </body>
    </html>
      

  4.   

    事件中的this就是触发事件的dom元素。
    <html>
    <head>
        <title>test</title>
        <script>
    function doOver() {
    this.style.backgroundColor = "Green";
    }
    function doOut(divId) {
    this.style.backgroundColor = "Blue";
    }
    onload = function() { // window.onload
    for (var i = 1; i <= 3; i++) {
    var div = document.getElementById("div" + i);
    div.onmouseover = doOver;
    div.onmouseout = doOut;
    }
    }
        </script>
    </head>
    <body>
      <div id="div1">one</div>
      <div id="div2">two</div>
      <div id="div3">three</div>
    </body>
    </html>
      

  5.   

    如果不用this,就得用闭包处理,参考如下实现:
    <html>
    <head>
        <title>test</title>
        <script>
            function doOver(divId) {
                document.getElementById(divId).style.backgroundColor = "Green";
            }
            function doOut(divId) {
                document.getElementById(divId).style.backgroundColor = "Blue";
            }

    onload = function() { // window.onload
    for (var i = 1; i <= 3; i++) {
    var id = "div" + i;
    var div = document.getElementById(id);
    div.onmouseover = (function(id) {
    return function() {
    doOver(id);
    }
    })(id);
    div.onmouseout = (function(id) {
    return function() {
    doOut(id);
    }
    })(id);
    }
    }
        </script>
    </head>
    <body>
      <div id="div1">one</div>
      <div id="div2">two</div>
      <div id="div3">three</div>
    </body>
    </html>
      

  6.   

    那像这种的该怎么写呢?
    <script type="text/javascript">
    <!--
            function PlayeCurrentPosition(obj, v) {
                window.parent.playerframe.PCP(obj, v);
            }        function Transformcolor(obj, eventstate) {
                window.parent.playerframe.TFC(obj, eventstate);
            }
    -->
        </script>html中有多个这样的div:<div id="div1" class="bgcolor1" onmouseover="Transformcolor(this,1);" onmouseout="Transformcolor(this,2);"
                onclick="PlayeCurrentPosition(this,'00:00:01');"></div>
      

  7.   

    <div id="div1" class="bgcolor1" eventstate="00:00:01"></div>
    <div id="div2" class="bgcolor1" eventstate="00:10:01"></div>
    <div id="div3" class="bgcolor1" eventstate="00:20:01"></div>
    <div id="div4" class="bgcolor1" eventstate="00:30:01"></div><script>
            onload = function() { // window.onload
                for (var i = 1; i <= 4; i++) {
                    var div = document.getElementById("div" + i);
                    div.onmouseover = function() {
                        window.parent.playerframe.PCP(this, 1);
                    };
                    div.onmouseout = function() {
                        window.parent.playerframe.PCP(this, 2);
                    };
                    div.click = function() {
                        window.parent.playerframe.TFC(this, this.eventstate);
                    }
                }
            }
        </script>
      

  8.   

    少写一个on
                    div.onclick = function() {
                        window.parent.playerframe.TFC(this, this.eventstate);
                    }
    新的问题新开贴,此帖不再回了。
    别再给我发私信,我只是蹭分而已。