


js template engine handlebars.js practical tutorial??Using index in loop_html/css_WEB-ITnose
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <META http-equiv=Content-Type content="text/html; charset=utf-8"> 5 <title>关于循环中索引的使用 - by 杨元</title> 6 </head> 7 <body> 8 <h1>关于循环中索引的使</h1> 9 <!--基础html框架-->10 <table>11 <thead>12 <tr>13 <th>序号</th>14 <th>姓名</th>15 <th>性别</th>16 <th>年龄</th>17 </tr>18 </thead>19 <tbody id="tableList">20 21 </tbody>22 </table>23 24 <!--插件引用-->25 <script type="text/javascript" src="script/jquery.js"></script>26 <!--注意!这个例子用的是新版本handlebars,旧版本不支持-->27 <script type="text/javascript" src="script/handlebars-v1.3.0.js"></script>28 29 <!--Handlebars.js模版-->30 <!--Handlebars.js模版放在script标签中,保留了html原有层次结构,模版中要写一些操作语句-->31 <!--id可以用来唯一确定一个模版,type是模版固定的写法-->32 <script id="table-template" type="text/x-handlebars-template">33 {{#each student}}34 <tr>35 <td>{{addOne @index}}</td>36 <td>{{name}}</td>37 <td>{{sex}}</td>38 <td>{{age}}</td>39 </tr>40 {{/each}}41 </script>42 43 <!--进行数据处理、html构造-->44 <script type="text/javascript">45 $(document).ready(function() {46 //模拟的json对象47 var data = {48 "student": [49 {50 "name": "张三",51 "sex": "0",52 "age": 1853 },54 {55 "name": "李四",56 "sex": "0",57 "age": 2258 },59 {60 "name": "妞妞",61 "sex": "1",62 "age": 1963 }64 ]65 };66 67 //注册一个Handlebars模版,通过id找到某一个模版,获取模版的html框架68 //$("#table-template").html()是jquery的语法,不懂的童鞋请恶补。。。69 var myTemplate = Handlebars.compile($("#table-template").html());70 71 //注册一个Handlebars Helper,用来将索引+1,因为默认是从0开始的72 Handlebars.registerHelper("addOne",function(index,options){73 return parseInt(index)+1;74 });75 76 //将json对象用刚刚注册的Handlebars模版封装,得到最终的html,插入到基础table中。77 $('#tableList').html(myTemplate(data));78 });79 </script>80 </body>81 </html>
The sequence number refers to: during the loop process, the index of the current loop. To put it bluntly, it is the current index of the loop. The number of loops usually starts from 0, the same is true in Handlebars.js.
What’s the use? The most common thing is to add a serial number to each row when displaying the table to clearly show how many records there are on the page, as in the example above.
In addition, in order to distinguish different IDs in the loop, Xiaocai just needs a serial number to ensure that the IDs are not repeated. Children's shoes who have used bootstrap should know that many events can be located by ID, such as the Listgroup control.
Without further ado, in Handlebars.js, you can get the current index through {{@index}}, which means that the @index variable represents the current index.
In the above example, it is not good for the table serial number to start from 0, so a Helper is registered and the index is 1.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.
