document.getElementById 用于 jQuery 中的变量元素

问题描述 投票:0回答:1

我似乎遇到了一个可能是新问题的问题(我在任何地方都找不到解决方案)

我有一个 jQuery 插件,它在一个对象中获取 4 个变量,其中 Geocoords 作为值(50.3675658° 或 -23.73456°),我用

$('#map').rectangle(qsparam);

调用我的函数

'#map' 是 div 的 ID,其中 Google 地图为我提供了自定义地图,并在我在对象中给出的 2 个点周围有边框。

这是我的插件:

(function($) {
    $.fn.rectangle = function(param) {
        var map, rectangle, coords = param || {};
        var southwest = new google.maps.LatLng(coords.s || -10, coords.w || -10);
        var northeast = new google.maps.LatLng(coords.n || 10, coords.e || 10);
        return this.each(function() {
            map = new google.maps.Map(document.getElementById('map'), {
                'mapTypeId' : google.maps.MapTypeId.TERRAIN
            })
            var bounds = new google.maps.LatLngBounds(southwest, northeast);
            map.fitBounds(bounds);
            rectangle = new google.maps.Rectangle({
                map : map,
                strokeColor : "#008DCF",
                strokeOpacity : 0.6,
                strokeWeight : 4,
                fillColor : "#B8B23B",
                fillOpacity : 0.18,
                Bounds : bounds
            });
        });
    }
})(jQuery);

但是我想保留其他div的插件变量,所以你能帮我将document.getElementById('map')更改为类似document.getElementById(div的名称我不知道atm)的东西吗,这样当我想将它用于具有其他名称的 div 时,我不需要更改 ma 插件?

jquery google-maps google-maps-api-3 getelementbyid
1个回答
1
投票

使用

this
代替
document.getElementById('map')

return this.each(function() {
    map = new google.maps.Map(this, {'mapTypeId' : google.maps.MapTypeId.TERRAIN});
    // ...
© www.soinside.com 2019 - 2024. All rights reserved.