gpt4 book ai didi

JavaScript 代码不工作,我不确定为什么

转载 作者:行者123 更新时间:2023-11-30 12:51:55 27 4
gpt4 key购买 nike

这是 HTML:

<!doctype html>
<head>
<title>Food Saver</title>
<script src="mycode.js"></script>
</head>

<body>
<form>
<input type="text" id="foodInput" placeholder="Enter food">
<input type="button" id="addFood" value="Add Food">
</form>
<ul id="foodList">
</ul>
</body>
</html>

这是链接到它的 JavaScript:

window.onload=init;
function init(){
var addFoodButton = getElementById("addFood");
addFoodButton.onclick = handleAddFoodButtonClick;
}
function handleAddFoodButtonClick(){
var textInput=getElementById("foodInput");
var food = textInput.value;

if(food===""){
alert("Please enter a food");
}
else{
var li = document.createElement("li");
li.innerHTML=food;
var ul = getElementById("foodList");
ul.appendChild(li);
}
}

为什么当我尝试提交食物值或空字符串时没有任何反应,我的代码中有什么错误

最佳答案

getElementById 应该是 document.getElementById:

window.onload=init;
function init(){
var addFoodButton = document.getElementById("addFood");
addFoodButton.onclick = handleAddFoodButtonClick;
}
function handleAddFoodButtonClick(){
var textInput = document.getElementById("foodInput");
var food = textInput.value;

if(food===""){
alert("Please enter a food");
}
else{
var li = document.createElement("li");
li.innerHTML=food;
var ul = document.getElementById("foodList");
ul.appendChild(li);
}
}

关于JavaScript 代码不工作,我不确定为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20672146/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com