Introduction and application of the template function of Underscore. js

Source: Internet
Author: User

Underscore is a very practical JavaScript library that provides support for many functions required for programming. It provides many practical functions without extending any JavaScript native objects.
Whether you write a small js code or a large HTML5 application, underscore can help. Currently, underscore is widely used. For example, the only library with strong backbone. js dependency is underscore. js.
Today we will mainly discuss the front-end template function of Underscore. Its template function is the same as the javascript front-end template described in the previous article. Data processing is more convenient. I wrote a small example for your reference.

Download complete instance
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8"/>
<Title> Underscore </title>
<Meta name = "viewport" content = "width = device-width, minimum-scale = 1.0, maximum-scale = 1.0, user-scalable = no"/>
<Meta name = "apple-mobile-web-app-capable" content = "yes"/>
<Meta name = "format-detection" content = "telephone = no"/>
<Link href = "index.css" rel = "stylesheet" type = "text/css"/>
<Script src = "jquery. js"> </script>
<Script src = "underscore. js"> </script>
</Head>
<Body>
</Body>
</Html>
<! -- Ace-template demo -->
<Script id = "t2" type = "text/template">
<% _. Each (datas, function (item) {%>
<Div class = "outer">
<Div class = "title">
<Span> <% = item. film %> </span>
</Div>
<Ul class = "ul">
<% _. Each (datas, function (item) {%>
<Li>
<A href = "<% = item. url %>"> [<% = item. title %>] </a>
</Li>
<%}); %>
</Ul>
</Div>
<%}); %>
</Script>
<! -- Data -->
<Script>
Var datas = [
{
Title: "1942 ",
Url: "http://www.jb51.net ",
Film: "Movie 1"
},
{
Title: "youth's drifting ",
Url: "http://www.jb51.net ",
Film: "movie 2"
},
{
Title: "Godfather ",
Url: "http://www.jb51.net ",
Film: "movie 3"
},
{
Title: "Xiao shenke's salvation ",
Url: "http://www.jb51.net ",
Film: "movie 4"
},
{
Title: "3d2012 ",
Url: "http://www.jb51.net ",
Film: "movie 5"
}
];
$ ("Body" ).html (_. template ($ ("# t2" ).html (), datas ));
</Script>
<! -- Click the drop-down event -->
<Script type = "text/javascript">
$ ('. Ul'). hide ();
$ ('. Ul> li: last-child'). addClass ('Last-li ');
$ ('Body> div: first-child> ul '). show ();
$ ('. Title'). click (function (){
$ (This). siblings (). toggle ();
$ (This). parent (). siblings (). children ('. bbs-nav-ul'). hide ();
}) $ ('. Title'). hover (function (){
$ (This). toggleClass ('hover ');
})
$ ('. Ul> li'). hover (function (){
$ (This). toggleClass ('hover ');
})
</Script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.