gpt4 book ai didi

javascript - 无法使用单独的按钮调用函数两次以上

转载 作者:行者123 更新时间:2023-12-03 04:04:43 25 4
gpt4 key购买 nike

我将具有相同功能的不同参数分配给不同的按钮。当我按下第三个按钮来调用此函数时,它不起作用。这是 JavaScript 的限制吗?或者有什么办法可以做到这一点?

HTML:

<head>
<link rel='stylesheet' type='text/css' href='css/styles.css'>
</head>

<body>

<p id="para">dud</p>

<button type="button" onclick="monsterFound();">Test monster</button>
<button type="button" onclick="d20();">Test a Die 20</button>
<button type="button" onclick="showChance();">Test Chance</button>
<button type="button" onclick="d20(); damageAccuracy(damage,dice1,calculateChance());">Test Accuracy</button>
<button type="button" onclick="equip(loot[1]);">equip weapon1</button>
<button type="button" onclick="equip(loot[5]);">equip weapon2</button>
<button type="button" onclick="equip(loot[8]);">equip weapon3</button>

<script src='https://code.jquery.com/jquery-3.1.0.min.js'></script>
<script src='main.js'></script>
</body>

JS:

var loot = [ 'rusty iron dagger', 'rusty iron claymore', 'rusty iron axe', 
'rusty iron hammer', 'rusty iron spear', 'rusty iron warhammer', 'rusty iron
waraxe', 'rusty iron short sword', 'rusty iron long sword', 'rusty iron
helmet', 'rusty iron breast-plate', 'rusty iron greaves', 'rusty iron left
pauldron', 'rusty iron right pauldron'];
// x = 14;
var onHandOut = 0;
var offHandOut = 0;
var onHand = "empty"; // right hand
var offHand = "empty"; // left hand
var damage = 0;
// when you equip a weapon, base damage is set -------------------
alert(onHand);
alert(damage);

function equip(item) {
onHand = item;

if (onHand === "empty") {
alert("Empty");
onHandOut = 0.65;
damage = onHandOut;

} else if (onHand === "rusty iron dagger") {
onHandOut = 1.25;
damage = onHandOut;
alert("Dagger equiped, Damage = " + onHandOut);

} else if (onHand === "rusty iron claymore") {
onHandOut = 2.25;
damage = onHandOut;
alert("claymore equiped, Damage = " + onHandOut);

} else if (onHand === "rusty iron axe") {
onHandOut = 1.25;
damage = onHandOut;
alert("axe equiped, Damage = " + onHandOut);

} else if (onHand === "rusty iron hammer") {
onHandOut = 1.5;
damage = onHandOut;
alert("hammer equiped, Damage = " + onHandOut);

} else if (onHand === "rusty iron spear") {
onHandOut = 1.75;
damage = onHandOut;
alert("spear equiped, Damage = " + onHandOut);

} else if (onHand === "rusty iron warhammer") {
onHandOut = 2.5;
damage = onHandOut;
alert("warhammer equiped, Damage = " + onHandOut);

} else if (onHand === "rusty iron waraxe") {
onHandOut = 2.25;
damage = onHandOut;
alert("waraxe equiped, Damage = " + onHandOut);

} else if (onhand === "rusty iron short sword") {
onHandOut = 2.25;
damage = onHandOut;
alert("short sword equiped, Damage = " + onHandOut);

} else if (onHand === "rusty iron long sword") {
onHandOut = 2.5;
damage = onHandOut;
alert("long sword equiped, Damage = " + onHandOut);

} else if (onhand === "empty") {
onHandOut = 0.625;
damage = onHandOut;
alert("no weapon equiped, Damage = " + onHandOut);

} else {
// Do nothing!
}

}

最佳答案

实际上,这只是您代码中的一个拼写错误(onhand 而不是 onHand)。

这条线在哪里

} else if (onhand === "rusty iron short sword") {

应该是:

} else if (onHand === "rusty iron short sword") {

关于javascript - 无法使用单独的按钮调用函数两次以上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44609393/

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