我的JavaScript代码抛出一个SyntaxError(Unexpected token<)

时间:2013-04-25 14:01:43

标签: backbone.js

任何人都可以帮助我找到我们的问题,我的代码发生了什么......

我收到的错误是:

Uncaught SyntaxError: Unexpected token < 

我的代码在这里:

$(function() {

    var userDetails=[
        {firstName:'Lakshmi', lastName:'Narayanan',age:32},
        {firstName:'Harish', lastName:'Manickam',age:28},
        {firstName:'Madan', lastName:'Gopal',age:27}
    ]

    var userModel = Backbone.Model.extend({
        defaults:{
            firstName:"",
            lastName:"",
            age:""
        }
    });

    var userList = Backbone.Collection.extend({
        model:userModel
    });

    var userView = Backbone.View.extend({
        tagName:"tr",
        className:"userList",
        template: $("#listTempalate").html(),
        render:function(){
            var temp = _.template(this.template);
            this.$el.html(temp(this.model.toJSON()));
            return this;
        }
    });

    var usersView = Backbone.View.extend({
        el:"tbody",
        initialize:function(){
            this.collection = new userList(userDetails);
            this.render();
        },
        render:function(){
            var that = this;
            _.each(this.collection.models, function(item){
                that.$el.append(new userView({model:item}).render().el);
            })
        }
    });

    var Router = Backbone.Router.extend({
        routes:{
            '' : 'home'
        }
    });

    var router = new Router();
    router.on('route:home', function(){
        var defaultUser = new usersView();
    })

    Backbone.history.start();

});

我的HTML:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,Chrome=1" />
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1">
    <title>User Manager</title>
    <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
</head>
<body>
    <div class="container">
        <h1>User Manager</h1>
        <hr>
        <div class="page">
            <table class="table striped">
                <thead>
                    <tr>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Age</th>
                        <th>Edit</th>
                    </tr>
                </thead>
                <tbody id="insertRows">

                </tbody>
            </table>
        </div>
    </div>
    <script id="listTempalate" type="text/template">
        <td><%= firstName %></td>
        <td><%= lastName %></td>
        <td><%= age %></td>
        <td><%= <a hre="#/edit/<%= user.id %>" class="btn">Edit</a></td>
    </script>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min.js"></script>
    <script type="text/javascript" src="js/userManager.js"></script>
</body>
</html>

说真的,我无法在这里找到我的问题来修复它。以及任何人建议我在网上找到backbone.js的问题..

所以让我继续检查我的代码..

提前致谢..

1 个答案:

答案 0 :(得分:2)

非常确定它与您的HTML

中的这一行有关
<td><%= <a hre="#/edit/<%= user.id %>" class="btn">Edit</a></td>

您打开<%=,然后再次打开/edit/<%=,我认为这会导致问题。即使允许打开两次,您也没有在该行中添加最终%>

玩弄它,让我们知道它是怎么回事。

修改

试试这个

<td> <a href="#/edit/<%= user.id %>" class="btn">Edit</a></td>

您不需要将整个内容包装在<%%>标记中,只需要输出您想要输出的部分。