简体   繁体   English

如何在不同的 HTML 元素上重用 JS 函数

[英]How to reuse a JS function on different HTML elements

I have 2 squares in a <table> and I want that when I click one, it become black and the other don't.我在<table>有 2 个方块,我希望当我点击一个时,它会变成黑色,而另一个则不会。

My current code is working but I gave them the same function and id , so when I click the first, it work well, but when I click the second, it's the first one that changes.我当前的代码正在运行,但我给了它们相同的 function 和id ,所以当我点击第一个时,它运行良好,但是当我点击第二个时,它是第一个改变的。
I know whats wrong, but I don't know how to correct it without having to create a function for each id .我知道出了什么问题,但我不知道如何在不必为每个id创建函数的情况下更正它。

Let's say I want 100 squares, I won't write one function to each one, so what can I do?假设我想要 100 个方格,我不会为每个方格写一个函数,那我该怎么办?

 function myFunc() { var element = document.getElementById("cc"); element.classList.toggle("th"); }
 table, th, td { border: solid black 1px; width: 100px; height: 100px; } .th { background-color: black; }
 <table> <tr> <th onclick="myFunc(this)" id="cc"></th> <th onclick="myFunc(this)" id="cc"></th> </tr> </table>

First of all, and more important: id must be unique, thats why it is called id (identifier).首先,更重要的是: id必须是唯一的,这就是为什么它被称为 id(标识符)。

Said that, I will show you two options to solve your question:话虽如此,我将向您展示两个选项来解决您的问题:

1. The better option : 1.更好的选择
Don't add any inline (direct in HTML) onclick listener.不要添加任何内联(直接在 HTML 中) onclick侦听器。 Add a common class to all <th> , then in Javascript add a single listener to each element that has the class, and use this inside the function, since this scope will be the clicked element.为所有<th>添加一个公共类,然后在 Javascript 中为每个具有该类的元素添加一个侦听器,并在函数内部使用this ,因为this范围将是被点击的元素。

Example:示例:

 let allTh = document.querySelectorAll(".clickableTh").forEach(x => x.onclick = myFunc) function myFunc() { this.classList.toggle("th"); }
 table, th, td { border:solid black 1px; width:100px; height:100px; } .th { background-color:black; }
 <table> <tr> <th class="clickableTh"></th> <th class="clickableTh"></th> <th class="clickableTh"></th> <th class="clickableTh"></th> <th class="clickableTh"></th> <th id="cc" class="notClickableTh"></th> </tr> </table>

2. Keeping your current structure : 2. 保持你目前的结构
As said in the comments, you pass this as parameter ( onclick="myFunc(this)" ), then inside myFunc you don't need to find the element, you'll already have it in as parameter.正如评论中所说,您将this作为参数传递( onclick="myFunc(this)" ),然后在myFunc您不需要找到该元素,您已经将其作为参数传入。

Click below to see the snippet code of example点击下方查看示例代码片段

 function myFunc(elem) { elem.classList.toggle("th"); }
 table, th, td { border:solid black 1px; width:100px; height:100px; } .th { background-color:black; }
 <table> <tr> <th onclick="myFunc(this)"></th> <th onclick="myFunc(this)"></th> <th onclick="myFunc(this)"></th> <th onclick="myFunc(this)"></th> <th onclick="myFunc(this)"></th> <th id="cc"></th> </tr> </table>

For less markup you can bind the click event on all th对于不太标记,你可以在所有的点击事件绑定th

 var th = document.getElementsByTagName("th"); for (var i = 0; i < th.length; i++) { th[i].addEventListener("click", function() { this.classList.toggle("th"); }); }
 th, td { border: solid black 1px; width: 100px; height: 100px; } .th { background-color: black; }
 <table> <tr> <th></th> <th></th> <th></th> <th></th> </tr> </table>

Your function is storing the first square in a variable with the cc identifier.您的函数将第一个方块存储在一个带有cc标识符的变量中。

Instead, you should pass a reference of the element clicked and store that element in the variable.相反,您应该传递所单击元素的引用并将该元素存储在变量中。 That way, the element clicked will be toggled.这样,单击的元素将被切换。

function myFunc(ref) {
   var element = document.getElementById(ref.id).innerHTML;
   element.classList.toggle("th");
}

Notice that must also add this to the call of the function.请注意,还必须this添加到函数的调用中。

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

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