js在百度地图API设置回调函数中调用for循环变量问题
[code]for(vari=0;i<1;i++){for(varj=i+1;j<5;j++){vardriving=newBMap.DrivingRoute(map);...
[code]
for (var i=0;i<1;i++)
{
for (var j=i+1;j<5;j++)
{
var driving = new BMap.DrivingRoute(map);
var P1 = new BMap.Point(x[i],y[i]); //终点-西安
var P2 = new BMap.Point(x[j],y[j]); //终点-北京
driving.search(P1,P2);
driving.$$i = i;
driving.$$j = j;
driving.setSearchCompleteCallback(function (results) {
var output = results.getPlan(0).getDistance(false);
var dis = output;
document.getElementById("textout").innerHTML += this.$$i + " " + this.$$j + " : " + dis / 1000 + " ";
});
}
}
[/code]
如上代码,为何设置回调函数中this.$$i显示undefined. 展开
for (var i=0;i<1;i++)
{
for (var j=i+1;j<5;j++)
{
var driving = new BMap.DrivingRoute(map);
var P1 = new BMap.Point(x[i],y[i]); //终点-西安
var P2 = new BMap.Point(x[j],y[j]); //终点-北京
driving.search(P1,P2);
driving.$$i = i;
driving.$$j = j;
driving.setSearchCompleteCallback(function (results) {
var output = results.getPlan(0).getDistance(false);
var dis = output;
document.getElementById("textout").innerHTML += this.$$i + " " + this.$$j + " : " + dis / 1000 + " ";
});
}
}
[/code]
如上代码,为何设置回调函数中this.$$i显示undefined. 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询