1

我有下面的代码,但 javascript 似乎根本没有执行。

我在这里缺少什么吗?

<!doctype html>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>Backbonejs</title>
</head>
<body>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js">
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min.js">
    <script type="text/javascript">

        Person = Backbone.Model.extend({
            initialize: function(){
                console.log('hello world');
                this.bind('change:name', function(){
                    console.log(this.get('name') + "is now the val for name");
                });
            },
            defaults: {
                name: "Bob hope",
                height: "unknown"
            },
            validate: function(attributes){
                if(attributes.name == "Joe"){
                    return "Uh oh, you're name is Joe!";
                }
            }
        });

        var person = new Person();
        person.set({name: "Joe", height: "6 feet"});
    </script>

</body>
</html>
4

2 回答 2

3

您缺少结束script标签,它们是强制性的:

<script type="text/javascript" src="[...]underscore-min.js"></script>
<script type="text/javascript" src="[...]backbone-min.js"></script>

没有它们,看起来 Backbone 永远不会被加载。

于 2013-04-28T12:09:41.640 回答
-2

如果您在浏览器中没有任何错误,则意味着您必须在页面加载时执行此代码或包含 jQuery 库。

于 2013-04-28T12:10:32.147 回答