0
私は、次のjQueryプラグインを持って:「例外TypeError:aは未定義である」jQueryプラグインを使用している場合がない要素
$(function() {
$.fn.mapit = function (position, styles) {
var self = $(this);
var map = new google.maps.Map(self[0], {
center: position,
styles: styles
});
var marker = new google.maps.Marker({
position: position,
map: map
});
return $(this);
}
});
そして、私のようなものを使用してそれを呼び出す:
google.load("maps", "3", callback: function() {
$("div#office").mapit({ lat: 38.73972, lng: -9.144263 }, [{"featureType":"all"}]);
});
問題がときであります私は次のエラーを取得する私はmapitを呼び出し、ページには何のdiv#オフィスがありません:
TypeError: a is undefined
私は私のプラグインに何をすべきこれを避ける?コードとして
mapitコールの前に 'if($(" div#office ")。length> 0)'を追加しますか? https://stackoverflow.com/questions/31044/is-there-an-exists-function-for-jquery?rq=1 –
プラグイン内の 'typeof self'を確認して、何を得るかを確認してください。 – skobaljic