随笔 - 432  文章 - 0  评论 - 15  阅读 - 63万

Underscore.js 的模板功能

Underscore是一个非常实用的JavaScript库,提供许多编程时需要的功能的支持,他在不扩展任何JavaScript的原生对象的情况下提供很多实用的功能。

  无论你写一段小的js代码,还是写一个大型的HTML5应用,underscore都能帮上忙。目前,underscore已经被广泛使用,例如,backbone.js唯一强依赖的库就是underscore.js。

  Underscore.js学习资源:

  Underscore.js 官网:http://underscorejs.org/#each  

  Underscore.js 中文文档: http://www.css88.com/doc/underscore/ 

今天主要讨论Underscore 的前端模板功能。它的模板功能和前一篇介绍的javascript前端模板是一样的。对数据的处理更加方便。写了个小例,供大家参考学习。

完整实例可以通过这个网址下载:http://pan.baidu.com/share/link?shareid=2509652039&uk=688556984

复制代码
<!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>

<!--数据 -->
    <script>
        var datas = [
            {
                title: "一九四二",
                url: "http://www.baidu.com",
                film:"电影1"
            },
            {
                title: "少年派的漂流",
                url: "http://www.baidu.com",
                film:"电影2"
            },
            {
                title: "教父",
                url: "http://www.baidu.com",
                film:"电影3"
            },
            {
                title: "肖申克的救赎",
                url: "http://www.baidu.com",
                film:"电影4"
            },
            {
                title: "3d2012",
                url: "http://www.baidu.com",
                film:"电影5"
            }
        ];    
            
        $("body").html( _.template($("#t2").html(), datas));

    </script>

<!--点击下拉事件-->
    <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
复制代码

 

posted on   狼来了  阅读(383)  评论(0编辑  收藏  举报
编辑推荐:
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示