jQuery插件,无法定位ID

时间:2016-06-17 07:07:36

标签: jquery

你可以看到我将一个函数应用于这样的ID:

$("map_canvas").multimap({ });

这是函数,但是this显示[object Object]并且我认为它正在显示窗口,而不是我带有该ID的DIV ......

(function($) {

        $.fn.multimap = function( options ) {

            var container = $( this );

我做错了什么?

编辑:

当我尝试使用this时仍然出现错误:

(function($) {

        $.fn.multimap = function( options ) {



            // This is the easiest way to have default options.
            var settings = $.extend({
                // These are the defaults.
                locations: [
                    ['Address Title', 'Add Address Here', {lat: 53.008756, lng: -2.181422}]
                ],
                iconSize: { width: 70, height: 70 },
                zoom: 10,
                mapType: 'ROADMAP'

            }, options );

            var map;
            var bounds = new google.maps.LatLngBounds();

            // Override handles
            var i_iconSize = settings.iconSize;
            if (settings.iconSize.constructor !== Object) { 
                i_iconSize = { width: settings.iconSize, height: settings.iconSize };
            }


            function initialize() {
                var lat = parseFloat(settings.locations[0][2]['lat']);
                var lng = parseFloat(settings.locations[0][2]['lng']);
                var i_mapType;

                switch(settings.mapType) {
                    case "ROADMAP":
                        i_mapType = google.maps.MapTypeId.ROADMAP;
                        break;
                    case "SATELLITE":
                        i_mapType = google.maps.MapTypeId.SATELLITE;
                        break;
                    case "HYBRID":
                        i_mapType = google.maps.MapTypeId.HYBRID;
                        break;
                    case "TERRAIN":
                        i_mapType = google.maps.MapTypeId.TERRAIN;
                        break;
                }

                map = new google.maps.Map(
                this, {
                    // Initial center point, changes ones the markers are added though
                    center: new google.maps.LatLng(lat,lng),

                    // This doesn't seme to work, seems to just scale to fit all markers in at the moment
                    zoom: settings.zoom,

                    // Change the map type, not sure what the others are called
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                });

                for (i = 0; i < (settings.locations).length; i++) {
                    createMarker(settings.locations, i);
                }
            }

错误:

  

未捕获的TypeError:无法执行&#39; getComputedStyle&#39; on&#39; Window&#39;:参数1的类型不是&#39;元素&#39;。

2 个答案:

答案 0 :(得分:3)

在ID名称之前使用#在JQuery ex: $("#idname")中使用id="idname"选择元素

如果您使用了该课程,则在课程名称之前使用. ex: $(".classname")选择class="classname"所有元素

$("map_canvas").multimap({ });

TO

$("#map_canvas").multimap({ });

答案 1 :(得分:0)

$("map_canvas").multimap({ }); 

应该是

$("#map_canvas").multimap({ });

有关jquery选择器的更多信息http://www.w3schools.com/jquery/jquery_ref_selectors.asp