Javascript 中的多个函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10867142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:20:39  来源:igfitidea点击:

Multiple Functions in Javascript

javascriptfunction

提问by user1433032

I'm sorry if this is a really stupid question, I'm a little new to JavaScript. I am trying to make a webpage containing multiple functions, but only the first of the functions will be successful. I searched on google and I only got results for calling multiple functions at once, but that is not what I am looking for. Here is an example of what I am trying to do:

如果这是一个非常愚蠢的问题,我很抱歉,我对 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>

Thanks for helping!

感谢您的帮助!

回答by Guffa

There is already a function named clearin the documentobject. Name your function something else.

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

回答by Musa

Your string has line breaks, you can remove them or add a \to the end of each line

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

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.";
}

? Edit: If you change the name of the clearfunction to say clearxit works, weird.

? 编辑:如果您更改clear函数的名称以clearx使其有效,则很奇怪。

Edit: Apparently there is a clear method in the document object

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

回答by Andy

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.";
}

回答by sachleen

Rename the clear()function to something else. I changed it to clearDesc()and it worked fine (after fixing the line break issue in the string). See here.

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

回答by Vinyl Windows

  <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>

Here is this code in a jsfiddle.net

这是 jsfiddle.net 中的这段代码

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

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