简体   繁体   中英

How do i use for loop for this code so,the class element changes?

I'm trying to use for loop this code:

document.querySelectorAll('[class^="machine1"]').forEach(p => {
  if (p.textContent === '') {
    p.textContent = partnumber;
    jobsheets();
  }
})      

so, it changes to machine 2, or machine 3. Stops once it reaches machine 3.

const machines = [1,2,3];

machines.forEach(function(m) {
    document.querySelectorAll(`[class^="machine${m}"]`)...
})

Define the machines you want to loop through, and then loop through them, changing your selector each time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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