所以我在html中制作了一个按钮,不知道为什么这不起作用

So I am making a button in html and not sure why this does not work

本文关键字:按钮 一个 不知道 不起作用 为什么 html      更新时间:2023-09-26

所以我试图创建一个按钮,当按下按钮时,它会创建一个新的div字符串,如node.innerHtml部分所示。然而,这个代码似乎不起作用,我不知道为什么。

这是html端的

<input type = "button" value = "start"  onclick="changeOne()">
<div id = "Checkifwon"> Answer </div> 

这是javascript端的

function changeOne(){
    var node=document.getElementById('Checkifwon');
    if(value() ){ //calls if true or false given by the other function
        node.innerHTML='<div id =Checkifwon > Sorry you died in one shot</div>';
    }
    else {
        node.innerHTML='<div id =Checkifwon > you lived another day</div>'; 
    }
}
function value(){
    var feww = Math.floor(Math.random() *2);
    if (feww === 0) {
        return false;
    }
    else {
        return true;
    }
}

我自己还没有运行代码,这不是最好的方法,但请尝试:

function changeOne() {
    var node=document.getElementById('Checkifwon');
    if(value()){
        //calls if true or false given by the other function
        node.innerHTML = 'Sorry you died in one shot';
    } else {
       node.innerHTML = 'you lived another day'; 
    }
}