简体   繁体   English

谁能帮助我在JS中正确定义一个函数?

[英]Can anyone help me correctly define a function in JS?

I have created a button with HTML and am trying to run a function I programmed with JavaScript. 我用HTML创建了一个按钮,并试图运行用JavaScript编写的函数。 I cannot get the function to work and receive the error message: 我无法使该功能正常工作并收到错误消息:

reference error: myFunction is not defined 参考错误:未定义myFunction

Here is my code. 这是我的代码。 Can anyone help me define this function? 谁能帮我定义此功能?

....<button type="button" onclick="myFunction(shot)">...

<script lang="JavaScript">
var shot = Math.random();

if (shot < .001) {
    shot = 1;
} else if (shot < .18) {
    shot = 2;
} else if (shot < .5) {
    shot = 3;
} else if (shot < .84) {
    shot = 4;
} else if (shot < .94) {
    shot = 5;
} else if (shot < .991) {
    shot = 6;
} else {
    shot = 7;
};

function myFunction(x) {
    if (x === 1) {
        console.log("-2");
    } else if (x === 2) {
        console.log("-1");
    } else if (x === 3) {
        console.log("0");
    } else if (x === 4) {
        console.log("+1");
    } else if (x === 5) {
        console.log("+2");
    } else if (x === 6) {
        console.log("+3");
    } else {
        console.log("+4");
    }
};
</script>

That is most likely because the script is after your <button> in your HTML. 这很可能是因为脚本在HTML中<button>之后。 Place it above and everything should be fine. 将其放在上面,一切都应该没问题。

Because your button is before your script the button doesn't know your function. 因为您的按钮位于脚本之前,所以该按钮不知道您的功能。 When you press your button the function ins't defined yet. 当您按下按钮时,该功能尚未定义。 You have to place your script for the button. 您必须为按钮放置脚本。

Also it would be better to put type="text/javascript" in your <script> tag. 另外,最好在您的<script>标记中type="text/javascript"

Your script would be like this: 您的脚本将如下所示:

<script type="text/javascript">
var shot = Math.random();
if (shot < .001) {
    shot = 1;
} else if (shot < .18) {
    shot = 2;
} else if (shot < .5) {
    shot = 3;
} else if (shot < .84) {
    shot = 4;
} else if (shot < .94) {
    shot = 5;
} else if (shot < .991) {
    shot = 6;
} else {
    shot = 7;
};

function myFunction(x) {
    if (x === 1) {
        console.log("-2");
    } else if (x === 2) {
        console.log("-1");
    } else if (x === 3) {
        console.log("0");
    } else if (x === 4) {
        console.log("+1");
    } else if (x === 5) {
        console.log("+2");
    } else if (x === 6) {
        console.log("+3");
    } else {
        console.log("+4");
    }
};
</script>

<button type="button" onclick="myFunction(shot)">

I believe it is the call to your button onClick method that makes the problem. 我相信是导致您的按钮onClick方法调用的原因。

<button type="button" onclick="myFunction(shot)">

At that point in code the shot variable is not defined. 那时在代码中还没有定义shot变量。 You need to first declare the variable before usage. 您需要在使用前先声明变量。 Or better yet remove it from button call and defined it in your myFunction as below: 或者最好将其从按钮调用中删除,并在myFunction中对其进行定义,如下所示:

<script type="text/javascript">
  function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }

  function getShot() {
    return getRandomInt(1, 7);
  }

function myFunction() {
   var x  = getShot();
    if (x === 1) {
        console.log("-2");
    } else if (x === 2) {
        console.log("-1");
    } else if (x === 3) {
        console.log("0");
    } else if (x === 4) {
        console.log("+1");
    } else if (x === 5) {
        console.log("+2");
    } else if (x === 6) {
        console.log("+3");
    } else {
        console.log("+4");
    }
};
</script>

I'd do this using jQuery: 我会使用jQuery来做到这一点:

var shot;
$(document).ready(function () {
    shot = Math.random();

    if (shot < 0.001) {
        shot = 1;
    } else if (shot < 0.18) {
        shot = 2;
    } else if (shot < 0.5) {
        shot = 3;
    } else if (shot < 0.84) {
        shot = 4;
    } else if (shot < 0.94) {
        shot = 5;
    } else if (shot < 0.991) {
        shot = 6;
    } else {
        shot = 7;
    }
});

$("#myButton").click(function () {
    var x = shot;
    if (x === 1) {
        console.log("-2");
    } else if (x === 2) {
        console.log("-1");
    } else if (x === 3) {
        console.log("0");
    } else if (x === 4) {
        console.log("+1");
    } else if (x === 5) {
        console.log("+2");
    } else if (x === 6) {
        console.log("+3");
    } else {
        console.log("+4");
    }
});

With your button as so: 使用您的按钮:

<button id="myButton" type="button">Test</button>

Demo here 在这里演示

With this, however, your result is going to be the same per page load unless you do the calculation for 'shot' in the click function! 但是,这样做的结果是,除非您在click函数中对“ shot”进行计算,否则每页加载的结果将是相同的!

You could either put the code before the button 您可以将代码放在按钮之前

or 要么

Make the script to execute after the DOM loads by using 通过使用以下命令使脚本在DOM加载后执行

document.addEventListener("DOMContentLoaded", function(event) { 
 //put your function here
 }); 

Also, a good practice (separation of concern), is to not have have the function call on the html. 另外,一个好的做法(关注点分离)是在html上没有函数调用。 Instead you could add an event listener in your javascript. 相反,您可以在JavaScript中添加事件监听器。

document.getElementById("myButton").addEventListener("click", function(){
// your function here
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM