javascript 未捕获的类型错误:无法读取未定义的属性“html”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30086393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:38:53  来源:igfitidea点击:

Uncaught TypeError: Cannot read property 'html' of undefined

javascriptjqueryhtmlbackbone.jshandlebars.js

提问by Chuie

I am trying to have a google map display on my webpage using backbone.js and handlebars.js, but I can't get it to display. When I load the page, I get this error in my javascript console:

我正在尝试使用backbone.js 和handlebars.js 在我的网页上显示谷歌地图,但无法显示。当我加载页面时,我在我的 javascript 控制台中收到此错误:

Uncaught TypeError: Cannot read property 'html' of undefined

未捕获的类型错误:无法读取未定义的属性“html”

Does anyone know what I am doing wrong? Any and all suggestions welcome.

有谁知道我做错了什么?欢迎任何和所有建议。

index.html

索引.html

<!DOCTYPE html>   
<html>                                                                          
    <head>                                                                      
        {% load staticfiles %}                                                  
        <meta charset="utf-8">                                                  
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">          
        <meta http-equiv="cleartype" content="on">                  
        <link rel="stylesheet" href="{% static 'bootstrap/css/bootstrap.css' %}">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
        <script src="{% static 'bootstrap/js/bootstrap.js' %}"></script>        
        <script src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.4/underscore-min.js"></script>
        <script src="http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"></script>
        <script src="{% static 'django_project/js/handlebars.js' %}"></script>  
        <link href="https://fonts.googleapis.com/css?family=Raleway:400,700,200" rel="stylesheet" type="text/css">
        <script src="https://maps.googleapis.com/maps/api/js?v=3&sensor=false"></script>
    </head>
    <body>
        <script type="text/x-mustache-template" id="grid-12-template">          
            <div id="googleMapBox" class="box-content"></div>               
        </script>                                                               
        <script src="{% static 'django_project/js/django_project.js' %}"></script>
        <script>                                                                
            var GoogMap = new GoogleMap;                                    
            GoogMap.render();
        </script>  
    </body>
</html>

django_project.js

django_project.js

var template = function (name) {                                                
    var source = $('#' + name + '-template').html();                        
    return Handlebars.compile(source);                                      
};                                                                              

var GoogleMap = Backbone.View.extend({

    template: template('grid-12'),

    initialize: function() {},

    activate: function() {

        var mapOptions = {                                                      
            zoom: 8,
            center: new google.maps.LatLng(-34.397, 150.644),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        var domElement = this.$('#googleMapBox');
        this.map = new google.maps.Map(domElement.get(0), mapOptions);

    },                                                                          

    render: function() {

        this.$el.html(this.template(this));                             
        this.activate();

        return this;

    }

});

采纳答案by Chuie

for some reason this.$el.html(...) was causing the problem. Even if I specified el in the view it wouldn't work. this is the code that worked.

由于某种原因 this.$el.html(...) 导致了问题。即使我在视图中指定了 el 它也不起作用。这是有效的代码。

django_project.js

django_project.js

var template = function (name) {                                                
    var source = $('#' + name + '-template').html();                            
    return Handlebars.compile(source);                                          
};                                                                              

var GoogleMap = Backbone.View.extend({                                          
    el: $('#map-canvas'),                                                       
    template: template('grid-12'),                                              
    initialize: function () {                                                   
        this.render();                                                          
    },      

    activate: function () {                                                     
        var mapOptions = {                                                      
            zoom: 8,                                                            
            center: new google.maps.LatLng(-34.397, 150.644),                   
            mapTypeId: google.maps.MapTypeId.ROADMAP                            
        };                                                                      
        var domElement = $('#googleMapBox');                                    
        this.map = new google.maps.Map(domElement.get(0), mapOptions);          
    },                                                                          
    render: function () {                                                       
        $('#map-canvas').html(this.template(this));                             
        this.activate();                                                        
        return this;                                                            
    }                                                                           
});                                                                             

$(function () {                                                                 
    var GoogMap = new GoogleMap();                                              
}); 

index.html

索引.html

    ...
    <div id="map-canvas"></div>                                             
    <script type="text/x-mustache-template" id="grid-12-template">          
        <div id = "googleMapBox"                                            
        class = "box-content"                                               
        style = "height: 600px; background-color: #b0c4de;" > </div >          
    </script> 
    ...