鍍金池/ 問答/網(wǎng)絡(luò)安全  HTML/ 一個(gè)簡單的前端渲染小例子出錯(cuò),underscore.js的錯(cuò)誤

一個(gè)簡單的前端渲染小例子出錯(cuò),underscore.js的錯(cuò)誤

就是想用underscore.js寫一個(gè)小例子,Node去發(fā)送json數(shù)據(jù),content.html引用underscore和jquery,把json的數(shù)據(jù)讀取到html模版內(nèi)容里,但是出錯(cuò)了。

錯(cuò)誤提示:
clipboard.png

定位到代碼:
content.html:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        .header{
            width:980px;
            height:100px;
            margin:0 auto;
            background-color: #ccc;
            margin-bottom: 20px;
        }

        .content {
            width:980px;
            margin:0 auto;
        }
        .main {
            float: left;
            width:599px;
            margin-right: 20px;
            border-right:1px solid red;
        }
        .aside {
            float: left;
            width: 360px;
            height: 400px;
            background-color: #ccc;
        }
        h1 {
            text-align: center;
        }
        .grid {
            border-bottom: 1px solid #333;
            box-shadow: 1px 1px 1px #333;
            margin-bottom: 10px;
            border-radius: 10px
            padding: 10px;
            box-sizing: border-box;
        }
    </style>
</head>
<body>
    <div class="header"></div>
    <div class="content">
        <div class="main">
        </div>

        <div class="aside"></div>
    </div>

    <script type="text/template" id = "moban">
        <div class="grid">
            <h3><%= biaoti %></h3>
            <p>發(fā)布時(shí)間:<%= shijian %> 作者:<%= zuozhe %></p>
            <p><%= neirong %></p>
            <p><a href="">詳細(xì)</a></p>
        </div>
    </script>

    <script type = "text/javascript" src = "jquery-1.11.3.min.js"></script>
    <script type="text/javascript" src = "underscore.js"></script>

    <script type="text/javascript">
        //得到模版內(nèi)容
        var mobanstring = $("moban").html();
        var compiled = _.template(mobanstring);
        //發(fā)出AJAX請求
        $.get("/news",function (data,status) {
            for(var i = 0; i < data.length; i++) {
                var compiledString = compiled(data[i]);
                $(".main").append($(compiledString));
            }
        });
    </script>
</body>
</html>

Node后臺(tái),app.js

var express = require("express");

var app = express();

app.use(express.static("./public"));

var shujuku = [
    {
        "biaoti":"我是1號(hào)新聞??!我很開心啊",
        "shijian":"2017年11月14日09:21:03",
        "zuozhe":"考拉",
        "neirong":"<p>內(nèi)容啊內(nèi)容啊內(nèi)容啊內(nèi)容啊</p>"
    },
    {
        "biaoti":"我是2號(hào)新聞??!我很開心啊",
        "shijian":"2017年11月14日09:21:03",
        "zuozhe":"Bob",
        "neirong":"內(nèi)容啊內(nèi)容啊內(nèi)容啊內(nèi)容啊"
    },
    {
        "biaoti":"我是3號(hào)新聞??!我很開心啊",
        "shijian":"2017年11月14日09:21:03",
        "zuozhe":"Jack",
        "neirong":"內(nèi)容啊內(nèi)容啊內(nèi)容啊內(nèi)容啊"
    },
    {
        "biaoti":"我是4號(hào)新聞啊!我很開心啊",
        "shijian":"2017年11月14日09:21:03",
        "zuozhe":"hehe",
        "neirong":"內(nèi)容啊內(nèi)容啊內(nèi)容啊內(nèi)容啊"
    }
];

app.get("/news",function (req,res) {
    //相當(dāng)于send的時(shí)候發(fā)的是json數(shù)據(jù)
    res.json(shujuku);
});

app.listen(3000);

運(yùn)行結(jié)果:
clipboard.png
正確結(jié)果應(yīng)該顯示:

clipboard.png

undersocre.js 中文文檔地址:undersocre.js的template

回答
編輯回答
遺莣

var mobanstring = $("#moban").html();
你把#漏掉了

https://jsfiddle.net/papersna...

2017年2月25日 14:16