简体   繁体   English

Javascript:如何通过单击按钮执行操作?

[英]Javascript: How do I perform a action with the click of a button?

What is the proper syntax for clicking a button and having Javascript run whatever statement you have? 单击按钮并使Javascript运行您所拥有的任何语句的正确语法是什么? I did a google search but there is either multiple ways or people aren't explaining the parameters or functions very well. 我做了一个google搜索,但是有多种方法,或者人们没有很好地解释参数或功能。

Here is my code. 这是我的代码。 All I want to do is when I click "attack" on my "button", the monster will lose 10 hp. 我要做的就是当我在“按钮”上单击“攻击”时,怪物会损失10 hp。

 document.getElementById("attack").click(); = dragon.hp = dragon.hp - 10;

When you do .click() you are calling a function. 当您执行.click()您正在调用一个函数。 ; is how you end statements. 结束语句的方式。 You want to assign a function to the onclick property. 您想要一个功能分配给 onclick属性。

You want something like this: 您想要这样的东西:

document.getElementById("attack").onclick = function(){
    dragon.hp -= 10;
};

Better yet, you really want: 更好的是,您确实想要:

document.getElementById("attack").addEventListener('click', function(){
    dragon.hp -= 10;
});

PS dragon.hp -= 10; PS dragon.hp -= 10; is shorthand for dragon.hp = dragon.hp - 10; dragon.hp = dragon.hp - 10;简写dragon.hp = dragon.hp - 10;

If you did something like this: 如果您执行以下操作:

            <a ..... onClick="deductHpPoints()">...</a>

Then in your javascript: 然后在您的JavaScript中:

            Function deductHpPoints()
            {
              //deduct logic
             }

Would that work for you? 那对你有用吗? For something basic. 对于一些基本的东西。

Here's what I would do: 这就是我要做的:

<input type="button" value="Attack" onclick="attack()">

And then wherever your JavaScript lives: 然后,无论您的JavaScript生活在哪里:

function attack() {
    dragon.hp -= 10;
};

Actually, I would make the attack able to target different things: 实际上,我将使攻击能够针对不同的目标:

function attack(target) {
    target.hp -= 10;
};

Maybe some radio buttons to select the target: 也许有些单选按钮可以选择目标:

<input type="radio" name="monster" value="Dragon">
<input type="radio" name="monster" value="Beholder">
<input type="radio" name="monster" value="Minotaur">

Some code to find out which one was selected: 一些代码来找出选择了哪个:

function discoverSelected(buttonName) {
    var theArray = document.getElementsByName(buttonName);
    for (var i = 0; i < theArray.length; i++) {
        if (theArray[i].checked) {
            return theArray[i].value;
        };
    };
};

Compare that name to the array of monsters: 将该名称与怪物列表进行比较:

function whichMonster(monName) {
    for (var i = 0; i < monsters.length; i++) {
        if (monsters[i].name == monName) {
            return monsters[i];
        };
    };
};

Then lastly, a button to attack with! 最后,还有一个攻击按钮!

<input type="button" value="Attack" onclick="attack(whichMonster(discoverSelected("monster")))">

Just to extend the response to jQuery library, you can do this (after calling the jQuery library) 只是为了将响应扩展到jQuery库,您可以执行此操作(在调用jQuery库之后)

$("#attack").on('click', function(){
    dragon.hp -= 10;
});

or, in case the #attack button is created dynamically 或者,如果#attack按钮是动态创建的

$(document).on('click', "#attack", function(){
        dragon.hp -= 10;
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM