Javascript 识别具有相同功能的不同按钮;onclick";函数名

Javascript 识别具有相同功能的不同按钮;onclick";函数名,javascript,html,function,button,onclick,Javascript,Html,Function,Button,Onclick,我的HTML文件中有3个按钮具有相同的onclick函数名 <button id="name 1" onclick="choice()">John</button> <button id="name 2" onclick="choice()">Martin</button> <button id="name 3" onc

我的HTML文件中有3个按钮具有相同的
onclick
函数名

    <button id="name 1" onclick="choice()">John</button>
    <button id="name 2" onclick="choice()">Martin</button>
    <button id="name 3" onclick="choice()">Sam</button>

是否有一种方法可以使一个函数唯一,即使有一个共同的名称,这样当我单击该按钮时,它只运行一个与该按钮相关的函数(在我的例子中,单击John只会给他添加一个点,而不会给其他人添加一个点)。另外,如何更改
点的数量
属性,以便每次单击按钮时添加1点

,您可以向函数传递一个参数,然后处理该参数以执行特定操作。function语句如下所示:

choice(parameter){
  if(parameter == "John"){
    characters[0].points = characters[0].points++;
  } else if(parameter == "Martin"){
    characters[1].points = characters[1].points++;
  } else{
    characters[2].points = characters[2].points++;
  }
}
<button id="name 1" onclick="choice("John")">John</button>
<button id="name 2" onclick="choice("Martin")">Martin</button>
<button id="name 3" onclick="choice()">Sam</button>
你可以这样打电话:

choice(parameter){
  if(parameter == "John"){
    characters[0].points = characters[0].points++;
  } else if(parameter == "Martin"){
    characters[1].points = characters[1].points++;
  } else{
    characters[2].points = characters[2].points++;
  }
}
<button id="name 1" onclick="choice("John")">John</button>
<button id="name 2" onclick="choice("Martin")">Martin</button>
<button id="name 3" onclick="choice()">Sam</button>
约翰 马丁 山姆
像这样的东西会对你有所帮助。

是的,你可以为每个按钮创建一个运行独特的函数

希望这有助于:

一种方法是:-> 您可以将button元素解析为choice函数的参数

let字符=[
{name:document.getElementById(“名称1”).textContent,点:0},
{name:document.getElementById(“名称2”).textContent,点:0},
{name:document.getElementById(“名称3”).textContent,点:0}
];
功能选择(按钮){
const buttonCharacterObject=characters.find(obj=>obj.name==button.textContent);
buttonCharacterObject.points+=1;
console.log(字符)
}
约翰 马丁
山姆谢谢你的回复,这就是我想要的:)顺便说一句,代码非常简洁!感谢您的回复,请查看Rilla的答案,因为他们有一个很好的解决方案,将来可能会对您有所帮助:)