Javascript中的多个函数

Multiple Functions in Javascript

本文关键字:函数 Javascript      更新时间:2023-09-26

抱歉,如果这是一个非常愚蠢的问题,我对JavaScript有点陌生。 我正在尝试制作一个包含多个功能的网页,但只有第一个功能会成功。 我在谷歌上搜索,我只得到了一次调用多个函数的结果,但这不是我想要的。 以下是我正在尝试执行的操作的示例:

<html>
    <head>
        <script type="text/javascript">
            function frogger()
            {
                document.getElementById("descriptions").innerHTML="Frogger <br />Description: Get
                    the frog to the islands at the top of the screen without falling into the water or
                    getting hit by cars. <br />Controls: Up arrow key to move forward, down arrow key to
                    move backward, left arrow key to move left, and right arrow key to move right.";
            }
            function clear()
            {
                document.getElementById("descriptions").innerHTML=" ";
            }
        </script>
    </head>
    <body>
        <div id="descriptions" style="{height:100;}">
        </div>
        <div class="game" onmouseover="frogger()" onmouseout="clear()">
            <a href="/arcade/frogger.html"><img border="0" src="http://ggmedia.site50.net
/pics/frogger.PNG" height="100" width="100" /><br />Frogger</a>
        </div>
    </body>
</html>

感谢您的帮助!

document 对象中已经有一个名为 clear 的函数。将函数命名为其他名称。

字符串有换行符,您可以删除它们或在每行末尾添加'

function frogger()
{
    document.getElementById("descriptions").innerHTML="Frogger <br />Description: Get'
the frog to the islands at the top of the screen without falling into the water or'
getting hit by cars. <br />Controls: Up arrow key to move forward, down arrow key to'
move backward, left arrow key to move left, and right arrow key to move right.";
}

编辑:如果您更改clear函数的名称以表示它clearx工作,那就奇怪了。

编辑:显然文档对象中有一个清晰的方法

function frogger() {
    document.getElementById("descriptions").innerHTML="Frogger <br />Description: Get the frog to the islands at the top of the screen without falling into the water or getting hit by cars. <br />Controls: Up arrow key to move forward, down arrow key to move backward, left arrow key to move left, and right arrow key to move right.";
}

clear()函数重命名为其他名称。我将其更改为 clearDesc() 并且工作正常(修复字符串中的换行问题后)。看这里。

  <div class="game" onmouseover="frogger()" onmouseout="clearr()">mousee</div>
    <div id="descriptions"></div>
    <script type="text/javascript">
        var frogger = function () {
            this.innerHTML = ["Frogger <br />Description: Get}",
                    "the frog to the islands at the top of the screen without falling into the water or",
                    "getting hit by cars. <br />Controls: Up arrow key to move forward, down arrow key to",
                    "move backward, left arrow key to move left, and right arrow key to move right."].join('');
        }.bind(document.getElementById("descriptions"));
        //
        var clearr = function () {
            this.innerHTML = " ";
        }.bind(document.getElementById("descriptions"));
    </script>

这是 jsfiddle.net 中的代码

http://jsfiddle.net/gerst20051/6Neqv/