0

バックボーンアプリのHelloWorldの例を実行していますが、スクリプトsrcタグがヘッダー(予想される場所)ではなく本文に含まれている場合にのみ機能します。ヘッダーにそれらがあり、alert(el)を実行すると、undefinedと表示されます。ただし、本文にスクリプトsrcタグがあり、alert(el)を実行すると、それが object HTMLBodyElement表示され、helloworldの例が機能します。また、私が見ているチュートリアルでは、本体内にスクリプトsrcタグが含まれていることにも注意してくださいhttp://arturadib.com/hello-backbonejs/(これは私がそれを機能させる唯一の方法です)

何故ですか?

動作しません

 <!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);
4

1 に答える 1

2

これを行うとき:

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

<body>スクリプトがロードされると、DOMにが含まれます。特に、<body>これをヒットすると、次のようになります。

el: $('body')

あなたがこのようにそれをするとき:

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

<body>ヒットしたときはないので、el: $('body')バックボーンがアンラップした後elに終了します。undefined$('body')

次の違いについて混乱していると思います。

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

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

最初のものはすぐに機能を実行し、2番目のものはと同じです

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

DOMの準備ができたら関数を実行します。JavaScriptを次のように見せたいと思うかもしれません。

$(function() {
  var ListView = Backbone.View.extend({ /* ... */ });
  var listView = new ListView();
  // And now do something with listView
});
于 2012-09-23T01:43:34.883 に答える