gpt4 book ai didi

未成功调用 Javascript 函数

转载 作者:行者123 更新时间:2023-11-30 14:09:27 25 4
gpt4 key购买 nike

目前的想法是只显示“hello world!”检查按钮是否正确调用了函数。我试图从以前有效的代码中复制粘贴,检查括号、方括号以及任何可能破坏函数调用的内容。虽然在“hello world!”之后什么也没有。是回答这个问题所必需的,如果您对下面的其余代码提出建议,我将不胜感激。这个想法是接受用户输入,并根据字符串长度给出不同的结果。

function persona() {
document.write("hello world!");
var name, inputstr, sum = 0;
name = document.getElementById("input").value;
inputstr = name.length;
while (inputstr / 10 >= 1) {
if (inputstr = 11 || inputstr = 22) {
break;
}
sum += inputstr % 10;
inputstr = Math.floor(inputstr / 10);
if (inputstr = 11 || inputstr = 22) {
break;
}
}
switch (inputstr) {
case 1:
document.getElementById("results").innerHTML = "...";
break;;
case 2:
document.getElementById("results").innerHTML = "...";
break;;
case 3:
document.getElementById("results").innerHTML = "...";
break;;
case 4:
document.getElementById("results").innerHTML = "...";
break;;
case 5:
document.getElementById("results").innerHTML = "...";
break;;
case 6:
document.getElementById("results").innerHTML = "...";
break;;
case 7:
document.getElementById("results").innerHTML = "...";
break;;
case 8:
document.getElementById("results").innerHTML = "...";
break;;
case 9:
document.getElementById("results").innerHTML = "...";
break;;
case 11:
document.getElementById("results").innerHTML = "...";
break;;
case 22:
document.getElementById("results").innerHTML = "...";
break;;
}
}
body {
background-color: #a89b28;
color: #f0f1f6;
font-family: impact;
text-align: center;
}
<!DOCTYPE html>
<html>
<title>Number Personality Calculator</title>

<head>
<meta name="description" content="Input your name, and predict your personality type" />
<meta name="keywords" content="JavaScript, JS, numerology, personality" />
<meta name="author" content="[REDACTED], 18/02/2018" />
<meta charset="UTF-8">
</head>

<body>
<h1>PERSONALITY TYPE CHECKER</h1>
<p id="results">""</p>
<input type="text" id="input" placeholder="Juan Dela Cruz"><br>
<button onclick="persona()">Get your personality</button>
</body>

</html>

最佳答案

如果您只是想修复代码中的错误,那是因为您在 if 语句中使用了赋值运算符而不是比较运算符。

inputstr = x 更改为 inputstr == x

<!DOCTYPE html>
<html>
<title>Number Personality Calculator</title>

<head>
<meta name="description" content="Input your name, and predict your personality type" />
<meta name="keywords" content="JavaScript, JS, numerology, personality" />
<meta name="author" content="[REDACTED], 18/02/2018" />
<meta charset="UTF-8">
<style>
body {
background-color: #a89b28;
color: #f0f1f6;
font-family: impact;
text-align: center;
}
</style>
</head>

<body>
<h1>PERSONALITY TYPE CHECKER</h1>
<p id="results">""</p>
<input type="text" id="input" placeholder="Juan Dela Cruz"></br>
<button onclick="persona()">Get your personality</button>
<script>
function persona() {
document.write("hello world!");
var name, inputstr, sum = 0;
name = document.getElementById("input").value;
inputstr = name.length;
while (inputstr / 10 >= 1) {
if (inputstr == 11 || inputstr == 22) {
break;
}
sum += inputstr % 10;
inputstr = Math.floor(inputstr / 10);
if (inputstr == 11 || inputstr == 22) {
break;
}
}
switch (inputstr) {
case 1:
document.getElementById("results").innerHTML = "...";
break;;
case 2:
document.getElementById("results").innerHTML = "...";
break;;
case 3:
document.getElementById("results").innerHTML = "...";
break;;
case 4:
document.getElementById("results").innerHTML = "...";
break;;
case 5:
document.getElementById("results").innerHTML = "...";
break;;
case 6:
document.getElementById("results").innerHTML = "...";
break;;
case 7:
document.getElementById("results").innerHTML = "...";
break;;
case 8:
document.getElementById("results").innerHTML = "...";
break;;
case 9:
document.getElementById("results").innerHTML = "...";
break;;
case 11:
document.getElementById("results").innerHTML = "...";
break;;
case 22:
document.getElementById("results").innerHTML = "...";
break;;
}
}
</script>
</body>

</html>

关于未成功调用 Javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54752189/

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