简体   繁体   English

如何在多个元素上运行相同的 function?

[英]How to run same function on multiple elements?

I created a web page with 3 counters that can be triggered separately, but I have a problem with the count function I can't use it on the 3 counters.我创建了一个 web 页面,其中包含 3 个可以单独触发的计数器,但是我的计数 function 有问题,我无法在 3 个计数器上使用它。

I manage to retrieve the index of each button but I don't know how to use it to trigger each counter separately.我设法检索了每个按钮的索引,但我不知道如何使用它来分别触发每个计数器。

can you help me to solve this problem please?你能帮我解决这个问题吗?

Here is my code.这是我的代码。

 "use strict"; /* ___________________________Déclarer les variables et les fonctions______________________________ */ /* Déclaration des variables */ var item = document.querySelectorAll('.slider-item'); var str = document.querySelectorAll('.buttons--start'); var stp = document.querySelectorAll('.button--stop'); var start = 0; var end = 0; var diff = 0; var timer = 0; /* Fonction chrono */ function chrono(){ end = new Date(); diff = end - start; diff = new Date(diff); var sec = diff.getSeconds(); item.innerHTML = sec; timer = setTimeout("chrono()", 10); } /* Fonction pour déclancher le chrono */ function chronoStart(i){ start = new Date(); chrono(); console.log('start '+i); } /* Fonction pour arrêter et rénitialiser le chrono */ function chronoStop(i){ clearTimeout(timer) item[i].innerHTML = "0"; start = new Date(); console.log('stop '+i); } /* Fonction qui change la couleur du background */ function changeColor(i){ let bgColor = '#' + Math.random().toString(16).substr(-6); item[i].style.backgroundColor = bgColor; } /* ___________________________Écouter les événements__________________________________ */ /* Appeler la fonction qui déclanche le chrono lorsqu'on clique sur start*/ for (let i=0; i<item.length; i++) { str[i].addEventListener('click',(e)=>{ chronoStart(i); }); //currentPos = i; } /* Appeler la fonction qui arrête le chrono lorsqu'on clique sur stop*/ for (let i=0; i<item.length; i++) { stp[i].addEventListener('click',(e)=>{ chronoStop(i); }); } /* Appeler la fonction qui change la couleur du background lorsque l'évenement est déclancher*/ for (let i=0; i<item.length; i++) { item[i].addEventListener("click",(e)=>{ changeColor(i); }); }
 body { font-family: Arial, sans-serif; }.wrapper { /* display: flex; flex-flow: column wrap; position: fixed; left: 0; right: 0; top: 0; bottom: 0; padding: 50px; */ }.slider { display: flex; align-items: center; justify-content: center; flex-grow: 1; }.slider-item { font-size: 120px; font-weight: bold; padding: 15px; background-color: yellow; user-select: none; }.buttons { margin-top: 15px; text-align: center; }.buttons button { display: inline-block; padding: 15px; font-size: 16px; color: #fff; border: none; outline: none; background-color: #000; cursor: pointer; }
 <div class="wrapper "> <div class="slider"> <div class="slider-item">0</div> </div> <div class="buttons"> <button class="buttons--start" type="button">Start</button> <button class="button--stop" type="button">Stop</button> </div> </div> <div class="wrapper"> <div class="slider"> <div class="slider-item">0</div> </div> <div class="buttons"> <button class="buttons--start" type="button">Start</button> <button class="button--stop" type="button">Stop</button> </div> </div> <div class="wrapper"> <div class="slider"> <div class="slider-item">0</div> </div> <div class="buttons"> <button class="buttons--start" type="button">Start</button> <button class="button--stop" type="button">Stop</button> </div> </div>

The issue is because you have multiple instances of the same repeated HTML content yet within your logic you do not keep the relevant references to the correct elements when an event occurs.问题是因为您有多个相同重复 HTML 内容的实例,但在您的逻辑中,当事件发生时您没有保留对正确元素的相关引用。

A simpler way to achieve this would be to loop through the containing elements and find the relevant child elements to bind event handlers to, or to update the content of.实现此目的的一种更简单的方法是遍历包含元素并找到相关的子元素以将事件处理程序绑定到或更新其内容。

The simple version of this logic would be something like this:这个逻辑的简单版本是这样的:

 "use strict"; document.querySelectorAll('.chrono-wrapper').forEach(el => { const container = el; const item = el.querySelector('.slider-item'); const str = el.querySelector('.buttons--start'); const stp = el.querySelector('.button--stop'); var start = 0; var end = 0; var diff = 0; var timer = 0; function chrono() { end = new Date(); diff = end - start; diff = new Date(diff); var sec = diff.getSeconds(); item.innerHTML = sec; timer = setTimeout(chrono, 10); } function chronoStart() { start = new Date(); chrono(); } function chronoStop() { clearTimeout(timer) item.innerHTML = "0"; start = new Date(); } function changeColor(i) { let bgColor = '#' + Math.random().toString(16).substr(-6); item.style.backgroundColor = bgColor; } str.addEventListener('click', chronoStart); stp.addEventListener('click', chronoStop); });
 body { font-family: Arial, sans-serif; }.chrono-wrapper { /* display: flex; flex-flow: column wrap; position: fixed; left: 0; right: 0; top: 0; bottom: 0; padding: 50px; */ }.slider { display: flex; align-items: center; justify-content: center; flex-grow: 1; }.slider-item { font-size: 120px; font-weight: bold; padding: 15px; background-color: yellow; user-select: none; }.buttons { margin-top: 15px; text-align: center; }.buttons button { display: inline-block; padding: 15px; font-size: 16px; color: #fff; border: none; outline: none; background-color: #000; cursor: pointer; }
 <div class="chrono-wrapper"> <div class="slider"> <div class="slider-item">0</div> </div> <div class="buttons"> <button class="buttons--start" type="button">Start</button> <button class="button--stop" type="button">Stop</button> </div> </div> <div class="chrono-wrapper"> <div class="slider"> <div class="slider-item">0</div> </div> <div class="buttons"> <button class="buttons--start" type="button">Start</button> <button class="button--stop" type="button">Stop</button> </div> </div> <div class="chrono-wrapper"> <div class="slider"> <div class="slider-item">0</div> </div> <div class="buttons"> <button class="buttons--start" type="button">Start</button> <button class="button--stop" type="button">Stop</button> </div> </div>

As Rory pointed out while I was doodling this up, you have repeated markup but a single set of variables tracking the timers.正如Rory 在我涂鸦时指出的那样,您重复了标记,但只有一组变量跟踪计时器。

You could remedy this by iterating over the occurrences in the HTML and setting up distinct handlers for each.您可以通过遍历 HTML 中出现的事件并为每个事件设置不同的处理程序来解决此问题。 Here's one way you could do that:这是您可以这样做的一种方法:

 "use strict"; function init() { // get elements whose id begins with "slider-" const sliders = document.querySelectorAll('[id^=slider-]'); // set up event listeners for each occurrence: sliders.forEach(s => { // the setInterval/timeout identifier for this instance let timerId; // get references to the child elements we need const startButton = s.querySelector('.buttons--start'); const stopButton = s.querySelector('.button--stop'); const display = s.querySelector('.slider-item'); // add a start button click listener startButton.addEventListener('click', () => { // clear existing timer if already running clearTimeout(timerId); // capture the start time const start = new Date(); // function to update the display const update = () => { display.innerHTML = ((Date.now() - start) / 1000).toFixed(1); } // start a timer, update the timerId variable timerId = setInterval(update, 100) // add a listener for the stop button to kill this timer const stopHandler = () => clearInterval(timerId); stopButton.addEventListener('click', () => { stopHandler(); // only needs to run once; remove the listener stopButton.removeEventListener('click', stopHandler); }); }); }); } // kick it off init();
 body { font-family: Arial, sans-serif; }.wrapper { /* display: flex; flex-flow: column wrap; position: fixed; left: 0; right: 0; top: 0; bottom: 0; padding: 50px; */ }.slider { display: flex; align-items: center; justify-content: center; flex-grow: 1; }.slider-item { font-size: 120px; font-weight: bold; padding: 15px; background-color: yellow; user-select: none; }.buttons { margin-top: 15px; text-align: center; }.buttons button { display: inline-block; padding: 15px; font-size: 16px; color: #fff; border: none; outline: none; background-color: #000; cursor: pointer; }
 <div class="wrapper" id="slider-1"> <div class="slider"> <div class="slider-item">0</div> </div> <div class="buttons"> <button class="buttons--start" type="button">Start</button> <button class="button--stop" type="button">Stop</button> </div> </div> <div class="wrapper" id="slider-2"> <div class="slider"> <div class="slider-item">0</div> </div> <div class="buttons"> <button class="buttons--start" type="button">Start</button> <button class="button--stop" type="button">Stop</button> </div> </div> <div class="wrapper" id="slider-3"> <div class="slider"> <div class="slider-item">0</div> </div> <div class="buttons"> <button class="buttons--start" type="button">Start</button> <button class="button--stop" type="button">Stop</button> </div> </div>

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

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