简体   繁体   English

如何在不使用 forEach() 的情况下循环 HTML 元素?

[英]HOW TO LOOP HTML ELEMENTS WITHOUT USING forEach()?

I'm currently working with my web based system using nodejs and html.我目前正在使用 nodejs 和 html 使用基于 Web 的系统。

Is there any alternative way to loop html elements without using forEach?有没有其他方法可以在不使用 forEach 的情况下循环 html 元素?

I'm thinking about like this(this is just an example):我正在考虑这样(这只是一个例子):

 <% for(var ctr=0; ctr<arrayname.length;ctr++){ %>`
      `<input type="text">`
    `<%}%>`

I hope someone will answer my question.我希望有人能回答我的问题。 :) :)

There are many ways to iterate through 'iterables' in javascript as in any other language.与在任何其他语言中一样,有很多方法可以在 javascript 中迭代“iterables”。

Please take a look to this link:请看一下这个链接:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration

Here you will find examples of all the possible ways:在这里,您将找到所有可能方法的示例:

  • for statement声明
  • do...while statement do...while 语句
  • while statement while 语句
  • labeled statement带标签的语句
  • break statement中断语句
  • continue statement继续声明
  • for...in statement for...in 声明
  • for...of statement for...of 语句

I hope it helps.我希望它有帮助。 If it doesn't fit you, it would be nice to know what and why you want to achieve.如果它不适合您,那么知道您想要实现什么以及为什么要实现会很好。

It seems that your issue is that you need to be access your iterating variable, which is workable through a .forEach call as the second argument in the function provided to the .forEach call.您的问题似乎是您需要访问迭代变量,这可以通过.forEach调用作为提供给.forEach调用的函数中的第二个参数来使用。

Code below to show proof.下面的代码显示证据。

 $(function(){ var items = ['one','two','three']; items.forEach(function(item,ctr){ $('#testing').append('<div class="ctr-'+ctr+'">'+item+' CTR: '+ctr+'</div>'); }); })
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="testing"> </div>

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

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