Underscore.js 的模板功能介紹與應(yīng)用
更新時(shí)間:2012年12月24日 17:27:49 作者:
Underscore是一個(gè)非常實(shí)用的JavaScript庫(kù),提供許多編程時(shí)需要的功能的支持,他在不擴(kuò)展任何JavaScript的原生對(duì)象的情況下提供很多實(shí)用的功能,需要了解的朋友可以詳細(xì)參考下
Underscore是一個(gè)非常實(shí)用的JavaScript庫(kù),提供許多編程時(shí)需要的功能的支持,他在不擴(kuò)展任何JavaScript的原生對(duì)象的情況下提供很多實(shí)用的功能。
無(wú)論你寫一段小的js代碼,還是寫一個(gè)大型的HTML5應(yīng)用,underscore都能幫上忙。目前,underscore已經(jīng)被廣泛使用,例如,backbone.js唯一強(qiáng)依賴的庫(kù)就是underscore.js。
今天主要討論Underscore 的前端模板功能。它的模板功能和前一篇介紹的javascript前端模板是一樣的。對(duì)數(shù)據(jù)的處理更加方便。寫了個(gè)小例,供大家參考學(xué)習(xí)。
完整實(shí)例下載
<!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>
<!--數(shù)據(jù) -->
<script>
var datas = [
{
title: "一九四二",
url: "http://www.dbjr.com.cn",
film:"電影1"
},
{
title: "少年派的漂流",
url: "http://www.dbjr.com.cn",
film:"電影2"
},
{
title: "教父",
url: "http://www.dbjr.com.cn",
film:"電影3"
},
{
title: "肖申克的救贖",
url: "http://www.dbjr.com.cn",
film:"電影4"
},
{
title: "3d2012",
url: "http://www.dbjr.com.cn",
film:"電影5"
}
];
$("body").html( _.template($("#t2").html(), datas));
</script>
<!--點(diǎn)擊下拉事件-->
<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
無(wú)論你寫一段小的js代碼,還是寫一個(gè)大型的HTML5應(yīng)用,underscore都能幫上忙。目前,underscore已經(jīng)被廣泛使用,例如,backbone.js唯一強(qiáng)依賴的庫(kù)就是underscore.js。
今天主要討論Underscore 的前端模板功能。它的模板功能和前一篇介紹的javascript前端模板是一樣的。對(duì)數(shù)據(jù)的處理更加方便。寫了個(gè)小例,供大家參考學(xué)習(xí)。
完整實(shí)例下載
復(fù)制代碼 代碼如下:
<!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>
<!--數(shù)據(jù) -->
<script>
var datas = [
{
title: "一九四二",
url: "http://www.dbjr.com.cn",
film:"電影1"
},
{
title: "少年派的漂流",
url: "http://www.dbjr.com.cn",
film:"電影2"
},
{
title: "教父",
url: "http://www.dbjr.com.cn",
film:"電影3"
},
{
title: "肖申克的救贖",
url: "http://www.dbjr.com.cn",
film:"電影4"
},
{
title: "3d2012",
url: "http://www.dbjr.com.cn",
film:"電影5"
}
];
$("body").html( _.template($("#t2").html(), datas));
</script>
<!--點(diǎn)擊下拉事件-->
<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
您可能感興趣的文章:
- JavaScript模板引擎應(yīng)用場(chǎng)景及實(shí)現(xiàn)原理詳解
- JavaScript構(gòu)建自己的模板小引擎示例
- JavaScript模板引擎Template.js使用詳解
- JS Excel讀取和寫入操作(模板操作)實(shí)現(xiàn)代碼
- JS模板實(shí)現(xiàn)方法
- javascript輕量級(jí)模板引擎juicer使用指南
- NodeJS框架Express的模板視圖機(jī)制分析
- 詳解JavaScript ES6中的模板字符串
- 基于jQuery的AJAX和JSON實(shí)現(xiàn)純html數(shù)據(jù)模板
- Node.js的Web模板引擎ejs的入門使用教程
- JavaScript模板引擎實(shí)現(xiàn)原理實(shí)例詳解
相關(guān)文章
BootStrap模態(tài)框和select2合用時(shí)input無(wú)法獲取焦點(diǎn)的解決方法
在bootstrap的模態(tài)框里使用select2插件,會(huì)導(dǎo)致select2里的input輸入框沒(méi)有辦法獲得焦點(diǎn),沒(méi)有辦法輸入。怎么解決這個(gè)問(wèn)題呢?下面小編給大家?guī)?lái)了BootStrap模態(tài)框和select2合用時(shí)input無(wú)法獲取焦點(diǎn)的解決方法,一起看看吧2017-09-09JavaScript實(shí)現(xiàn)讀取上傳視頻文件的時(shí)長(zhǎng)和第一幀畫面過(guò)程講解
當(dāng)我們做一個(gè)后臺(tái)系統(tǒng)的音視頻管理模塊時(shí),通常要限制文件的大小和類型,這篇文章主要介紹了JavaScript實(shí)現(xiàn)讀取上傳視頻文件的時(shí)長(zhǎng)和第一幀畫面過(guò)程,需要詳細(xì)了解實(shí)現(xiàn)方法可以參考下文2023-05-05bootstrap多層模態(tài)框滾動(dòng)條消失的問(wèn)題
本篇文章主要介紹了bootstrap多層模態(tài)框滾動(dòng)條消失的問(wèn)題,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07javascript設(shè)計(jì)模式 – 適配器模式原理與應(yīng)用實(shí)例分析
這篇文章主要介紹了javascript設(shè)計(jì)模式 – 適配器模式,結(jié)合實(shí)例形式分析了javascript適配器模式相關(guān)概念、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04Firefox下提示illegal character并出現(xiàn)亂碼的原因
Firefox下提示illegal character并出現(xiàn)亂碼的問(wèn)題,時(shí)間是是因?yàn)榫幋a的問(wèn)題導(dǎo)致。2010-03-03JavaScript實(shí)現(xiàn)數(shù)組去重的十種方法分享
去重是開發(fā)中經(jīng)常會(huì)碰到的一個(gè)熱點(diǎn)問(wèn)題,這篇文章主要介紹了JavaScript中實(shí)現(xiàn)數(shù)組去重的10種方法,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-11-11