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

标签 javascript jquery html

我将具有相同功能的不同参数分配给不同的按钮。当我按下第三个按钮来调用此函数时,它不起作用。这是 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/

相关文章:

javascript - 使用 sencha touch 处理 google map 时遇到问题

javascript - 鼠标滚动在 Google Chrome 上不起作用

javascript - skrollr 在滚动条上添加/删除类

javascript - 为什么这个简单的 JavaScript 程序不能运行?

html - 制作登录/注册表单的最正确/简单/使用/最干净的方法(Divs/其他)

javascript - 如何使用 jquery 查找背景颜色为黄色的所有行?

javascript - 默认情况下无法将整个 div 内容设置为只读,并且在单击编辑按钮时无法启用编辑

javascript - 使用 PEG.js 忽略空格

javascript - 从 Javascript 创建 AngularJS Ng 元素

jquery - 将授权 header 中的 api key 发送到 REST Web API