我试图在下面的JavaScript中打印出latitude(lat)变量。如何在不同的功能中使用变量?
我该如何参考lat变量? this.lat, that.lat, vicinity.lat
等?
在我的javascript我有
var Vicinity = function (pubReference, sessionId, done, err) {
this.ad = {
getBannerHtml: function() {
console.log(this.lat); // how do I refer to the lat variable?
}
};
this.init = function (done, err) {
var that = this;
this.getLatLng(that, function (position) {
that.lat = position.coords.latitude;
}, err);
if (typeof done === 'function')
done(this);
};
this.init(done, err);
};
$(document).ready(function() {
var data = new Vicinity(pubReference, sessionId,
function (result) {
$("#sticky").html(result.ad.getBannerHtml());
}
);
});
如果你没有“自己的”内嵌框架,即它是在同一个域中父页面,则无法从内部它http://stackoverflow.com/questions/17142285/access-iframe访问变量-content-from-parent-jquery –
你错了,如果你认为getBannerHtml中的'this'指向'Vicinity'或由'Vicinity'创建实例,则指'this.ad'对象。所以,如果你将它缓存到变量'that'中会更好。看看[Demo](http://jsfiddle.net/GKDev/U5xZ4/) – Givi
函数getBannerHtml:function()我如何引用lat和lon变量?这个,那个.lat,nearby.ad.lat? – grabury