我使用相同的功能,装备分配不同的参数,但是分配给不同的按钮。当我按第三个按钮来调用此功能时,它不起作用。这是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!
}
}
答案 0 :(得分:1)
实际上,这只是代码上的错字( onhand 而不是 onHand )。
你有这条线的地方
} else if (onhand === "rusty iron short sword") {
应该是:
} else if (onHand === "rusty iron short sword") {