Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript投币_Javascript_Html - Fatal编程技术网

使用javascript投币

使用javascript投币,javascript,html,Javascript,Html,我试图在单击按钮时显示正面或反面。我不确定我错在哪里。这似乎是一件很简单的事情,但也许我错过了什么?这是我的 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Assignment 9</title> <link href="images/avatar.png" rel="short

我试图在单击按钮时显示正面或反面。我不确定我错在哪里。这似乎是一件很简单的事情,但也许我错过了什么?这是我的

 <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>Assignment 9</title> 
        <link href="images/avatar.png" rel="shortcut icon" type="image/png">
        <link href="css/stylesheet.css" rel="stylesheet" type="text/css">
        <script src="js/javascript.js" type="text/javascript"></script>
        <style type="text/css">
        .auto-style1 {
            text-align: center;
        }
        </style>
        <script type="text/javascript">
        </script>
    </head>
    <body>
    <header>
    </header>
    <aside>
    </aside>
    <div id="main">
    <h1> Arrays and Coditional Statements</h1>

    <h2 class="auto-style1"> Quote of the Day</h2>


    <h2 class="auto-style1">Flip a coin </h2>
    <script>
  <script>
function myFunction1{
var x =Math.floor(Math.random()*2);
if ( x==0){
document.getElementById("Coin").innerHTML= "Heads";
}
else{
document.getElementById("Coin").innerHTML= "Tails";
}}

myFunction1();



</script>
<button type="button" onclick="myFunction1" id="Coin"> CLick ME</button>


    </div>
    <footer>
    </footer>
    </body>
    </html>

作业9
.auto-style1{
文本对齐:居中;
}
数组和密码语句
今日名言
掷硬币
功能myFunction1{
var x=Math.floor(Math.random()*2);
如果(x==0){
document.getElementById(“Coin”).innerHTML=“Heads”;
}
否则{
document.getElementById(“Coin”).innerHTML=“Tails”;
}}
myFunction1();
点击我

myFunction1
之后,您忘记了
()
您没有在
onclick
属性中调用函数,您只是在命名它。调用函数的方法是将
()
(将参数括起来)放在函数名后面。应该如此

onclick="myFunction1()"
以下是解决方案:

作业9 .auto-style1{ 文本对齐:居中; } 数组和密码语句


有几件事:

  • 冗余
    script
    标记正好位于实际需要的标记下方
  • onclick
    调用中需要
    ()
    -
    onclick=“myFunction1()”
  • 在函数定义中还需要
    ()
    函数myFunction1(){
  • 在第一次调用
    myFunction1()
    时,您的
    Coin
    元素还不存在-将脚本放在该元素创建之后,或放在
    窗口中。onload
    处理程序中
  • 将这些修复组合在一起:

    点击我
    函数myFunction1(){
    var x=Math.floor(Math.random()*2);
    控制台日志(x);
    如果(x==0){
    document.getElementById(“Coin”).innerHTML=“Heads”;
    }否则{
    document.getElementById(“Coin”).innerHTML=“Tails”;
    }
    }
    myFunction1();
    
    您可以使用这一行JavaScript获取
    头部
    尾部

    console.log(Math.random() >= 0.5 ? "heads" : "tails")
    

    除了@Barmar的建议外,
    函数myFunction1{
    应该是
    函数myFunction1(){
    除了下面你写的两条之外,如果我想显示硬币侧面的图片,确定生成的数字是多少,我可以将图片添加到相应的if或else语句中吗?它似乎不起作用。我添加了一个img标记并设置了它的src,但当单击按钮时它不会显示图片
    myFunction1();
    
    function myFunction1() {
    var x =Math.floor(Math.random()*2);
    if ( x==0){
    document.getElementById("Coin").innerHTML= "Heads";
    }
    else{
    document.getElementById("Coin").innerHTML= "Tails";
    }
    }
    
    </div>
    <footer>
    </footer>
    </body>
    </html>
    
    console.log(Math.random() >= 0.5 ? "heads" : "tails")