javascript - 关于for循环始终显示最大值问题
PHP中文网
PHP中文网 2017-04-11 12:25:32
[JavaScript讨论组]

稍微了解了下,好像是闭包添加的问题,但尝试了好几种方法,没有能够成功保存每次循环的i值,有大神能告诉我下,应该怎么封装使返回函数中能获取到每个i值

    function translateCallback(point) {
    function SquareOverlay(center, length,height, Bimg){
        this._center = center;
        this._length = length;
        this._height = height;
        this._backgroundImg = Bimg;
    }
    // 继承API的BMap.Overlay
    SquareOverlay.prototype = new BMap.Overlay();
    // 实现初始化方法
    SquareOverlay.prototype.initialize = function(map){
        // 保存map对象实例
        this._map = map;
        // 创建p元素,作为自定义覆盖物的容器
        var p = document.createElement("p");
        p.innerHTML="

"+4+"

"; p.style.position = "absolute"; // 可以根据参数设置元素外观 p.style.width = this._length + "px"; p.style.height = this._height + "px"; p.style.background = this._backgroundImg; // 将p添加到覆盖物容器中 map.getPanes().markerPane.appendChild(p); // 保存p实例 this._p = p; // 需要将p元素作为方法的返回值,当调用该覆盖物的show、 // hide方法,或者对覆盖物进行移除时,API都将操作此元素。 return p; }; // 实现绘制方法 SquareOverlay.prototype.draw = function(){ // 根据地理坐标转换为像素坐标,并设置给容器 var position = this._map.pointToOverlayPixel(this._center); this._p.style.left = position.x - this._length / 2 + "px"; this._p.style.top = position.y - this._length / 2 + "px"; }; SquareOverlay.prototype.addEventListener = function(event,fun){ this._p['on'+event] = fun; } // 添加自定义覆盖物 var mySquare = new SquareOverlay(point, 28, 35, "url('tp/map-logo.png')no-repeat -28px -87px"); bm.addOverlay(mySquare); // mySquare.addEventListener("click", function(e){ bm.addEventListener('click',fo(e)); //给地图绑定点击事件 //给地图绑定点击事件 }); //叠加层点击事件 function fo(e){ var mContent=""; //var sContent = "

  • NO.0000000001

  • NO.0000000002

"; infoWindow = new BMap.InfoWindow(mContent); // 创建信息窗口对象 bm.openInfoWindow(infoWindow,point); //开启信息窗口 bm.removeEventListener('click', fo); //这里取消绑定。 } } // var bm = new BMap.Map("allmap"); var center= new BMap.Point('117.125575995835','36.6702207308909'); bm.centerAndZoom(center, 15); bm.addControl(new BMap.NavigationControl()); //GPS坐标 var xx = ['117.129575995835','117.121575995835','117.125575995835','117.113575995835']; var yy = ['36.6702207308909','36.6702207234109','36.6703247308909','36.6702102348909']; var nn= ['1','2','3','4']; //地图初始化 for(i=0;i

}

PHP中文网
PHP中文网

认证0级讲师

全部回复(1)
怪我咯

包一层自执行函数

for(var i = 0; i < 10; i++){
  setTimeout(function(){
    console.info(i);
  });
}

// 全输出 10
for(var i = 0; i < 10; i++){
  (function(index){
    setTimeout(function(){
      console.info(index);
    });
  })(i);
}
// 正常输出 i 的值
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号