为什么不是't点击按钮时执行的功能

Why isn't function performing on button click?

本文关键字:按钮 执行 功能 为什么不      更新时间:2024-01-04

由于某些原因,我无法在单击按钮时执行我的函数。我主要是在codeacademy.com上学习的,不幸的是,它们在现实世界中没有太多应用。

这是我的代码:

<script>
    var ayee = function() {
        confirm("Ready to play?");
        var age = prompt("How old are you?");
        if (age >= 18) {
            document.write("Let's get started then!");
        }else{
            document.write("You're under 18? Be careful out there....");
        }
    }               
</script>
<button type="button" onclick="ayee" value="click" />

非常感谢。

您需要用()调用函数。像这样:ayee()

但是,根本不应该使用内联js。相反,你应该这样做:

现场演示(点击)。

<button id="myButton">My Button</button>

JavaScript:

var myButton = document.getElementById('myButton');
myButton.addEventListener('click', ayee);
function ayee() {
  //do something
}

这里不要使用CCD_ 3。你几乎从不需要使用它,如果你使用了,请确保你真的使用了。

您在ayee:之后缺少()

<button type="button" onclick="ayee()" value="click" />

将函数命名为

function ayee() {
  //insert your code
}

点击后用附带调用函数

<button type="button" onclick="ayee()" value="click" />

这样就可以了。

函数调用上需要括号:

<button type="button" onclick="ayee()" value="click" />