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 instanceCopy 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