“body”未定义,具体取决于脚本标记的位置

时间:2012-09-23 00:52:26

标签: javascript backbone.js

我正在做一个骨干应用程序的hello world示例,它只适用于脚本src标记包含在正文中,而不是在标题中(我希望它们在那里)。如果我在标题中有它们并且发出警报(el)则表示未定义。但是,如果我在主体中有脚本src标记并且发出警报(el),它会显示object HTMLBodyElement并且hello world示例有效。我还注意到我正在查看的教程在正文http://arturadib.com/hello-backbonejs/中有脚本src标记(这是我可以让它工作的唯一方法)

为什么?

不起作用

 <!DOCTYPE html>
  <html>
  <head>
    <meta charset="utf-8">
    <title>hello-backbonejs</title>
    <link rel="stylesheet" href="static/style.css">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
  <script src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
  <script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
  <script src="http://documentcloud.github.com/backbone/backbone-min.js"></script>
  <script src="static/backbone.localStorage-min.js"></script>
  <script src="app.js"></script>

  </head>
  <body>



  </body>
  </html>

作品

 <!DOCTYPE html>
  <html>
  <head>
    <meta charset="utf-8">
    <title>hello-backbonejs</title>
    <link rel="stylesheet" href="static/style.css">


  </head>
  <body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
  <script src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
  <script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
  <script src="http://documentcloud.github.com/backbone/backbone-min.js"></script>
  <script src="static/backbone.localStorage-min.js"></script>
  <script src="app.js"></script>


  </body>
  </html>

这是javascript

(function($){
  // **ListView class**: Our main app view.
  var ListView = Backbone.View.extend({    
    el: $('body'), // attaches `this.el` to an existing element.
    // `initialize()`: Automatically called upon instantiation. Where you make all types of bindings, _excluding_ UI events, such as clicks, etc.
    initialize: function(){
      _.bindAll(this, 'render'); // fixes loss of context for 'this' within methods

       this.render(); // not all views are self-rendering. This one is.

    },
    // `render()`: Function in charge of rendering the entire view in `this.el`. Needs to be manually called by the user.
    render: function(){
      $(this.el).append("<ul> <li>hello world</li> </ul>");
      alert(this.el);
    }
  });

  // **listView instance**: Instantiate main app view.
  var listView = new ListView();      
})(jQuery);

1 个答案:

答案 0 :(得分:2)

执行此操作时:

<body>
   <script src="..."></script>
   ...
</body>

加载脚本时,DOM中会有<body>。特别是,当您点击此内容时,您将获得<body>

el: $('body')

当你这样做时:

<head>
    <script src="..."></script>
</head>
<body>
    ...
</body>

当您点击<body>时不会有el: $('body'),因此在Backbone取消el后,您的undefined将以$('body')结束。

我认为你对以下两者之间的区别感到困惑:

(function($) { /* ... */ })(jQuery);

$(function() { /* ... */ });

第一个立即执行该功能,第二个与

相同
$(document).ready(function() { /* ... */ });

并且在DOM准备好时执行该函数。您可能希望JavaScript看起来更像这样:

$(function() {
  var ListView = Backbone.View.extend({ /* ... */ });
  var listView = new ListView();
  // And now do something with listView
});