Introduction and application of underscore.js template function _javascript skills

Source: Internet
Author: User
Underscore is a very useful JavaScript library that provides support for many of the features needed for programming, and he offers a lot of practical functionality without expanding the native objects of any JavaScript.
Whether you write a little bit of JS code, or write a large HTML5 application, underscore can help. At present, underscore has been widely used, for example, backbone.js the only strong dependent library is underscore.js.
Today, we mainly discuss underscore front-end template features. Its template functionality is the same as the JavaScript front-end template described in the previous article. The processing of data is more convenient. Wrote a small example, for everyone to study the reference.

Full instance Download
Copy Code code as follows:

<! DOCTYPE html>
<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>
<body>
</body>
<!--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: "The Drift of the young school",
URL: "Http://www.jb51.net",
Film: "Movie 2"
},
{
Title: "Godfather",
URL: "Http://www.jb51.net",
Film: "Movie 3"
},
{
Title: "Shawshank Redemption",
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 dropdown 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

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.