1
<html>
    <head>
        <script language="Javascript">
            function changecolor(var c)
            {
            document.body.style.background=c;
            }
        </script>
    </head>
    <body>
        <table width="300" height="100" align="center" border=2>
            <tr>
                <td onmouseout=changecolor("transparent") onmouseover=changecolor("red")>red</td>
                <td onmouseout=changecolor("transparent") onmouseover=changecolor("green")>green</td>
                <td onmouseout=changecolor("transparent") onmouseover=changecolor("blue")>blue</td>
            </tr>
        </table>
    </body>
</html>

I am not getting the change of color. Can someone please help...! Thanks in advance.

4

5 回答 5

2

You should write

function changecolor(c)

instead of

function changecolor(var c)

Because when you write var c in the function declaration, the browser will declare a new variable in the scope of your function. So when you then use the c variable it will return undefined since you didn't assigned some data to the variable.

于 2013-08-17T07:08:37.037 回答
0

在你的 javascript 函数中不需要声明参数的类型

所以改变

function changecolor(var c)

function changecolor(c)

这将解决你的问题。

于 2013-08-17T07:11:03.173 回答
0
<html>
<head>
    <script language="Javascript">
        function changecolor(c)
        {
            document.body.style.backgroundColor=c;
        }
    </script>
</head>
<body>
    <table width="300" height="100" align="center" border=2>
        <tr>
            <td onmouseout="changecolor('white')" onmouseover="changecolor('red')">red</td>
            <td onmouseout="changecolor('white')" onmouseover="changecolor('green')">green</td>
            <td onmouseout="changecolor('white')" onmouseover="changecolor('blue')">blue</td>
        </tr>
    </table>
</body>

于 2013-08-17T07:26:12.717 回答
0

尝试这个:

 <script language="Javascript">
            function changecolor(c)//remove var
            {
            document.body.style.background=c;
            }
        </script>
于 2013-08-17T07:27:03.383 回答
0
<body>
<table width="300" height="100" align="center" border=2>
    <tr>
        <td id="redcolor" onmouseover="change_red()" onmouseout="change_color()">red</td>
    <td id="greencolor" onmouseover="change_green()" onmouseout="change_color()">green</td>
        <td id="bluecolor" onmouseover="change_blue()" onmouseout="change_color()">blue</td>
    </tr>
 </table>

 <script>  
     function change_red()
         {
             document.getElementById("redcolor").style.color="red";
         }

     function change_green()
        {
     document.getElementById("greencolor").style.color="green";
 }

function change_blue()
{
    document.getElementById("bluecolor").style.color="blue";
}

function change_color()
{
document.getElementById("redcolor").style.color="#000";
document.getElementById("greencolor").style.color="#000";
document.getElementById("bluecolor").style.color="#000";
}
</script>  

</body>
于 2013-08-17T07:47:07.250 回答