搜尋

首頁  >  問答  >  主體

javascript - 無法讀取未定義的屬性“addEventListener”

想在百度地圖裡用addEventListener監聽點擊marker,結果報錯提示Cannot read property 'addEventListener' of undefined

JS代碼

var id = <?php echo json_encode($id);?>;
var time = <?php echo json_encode($time);?>;
var height = <?php echo json_encode($height);?>;
var latitude = <?php echo json_encode($latitude);?>;
var longitude = <?php echo json_encode($longitude);?>;
var date = <?php echo json_encode($date);?>;
var speed = <?php echo json_encode($speed);?>;
var rownum = <?php echo json_encode($rownum);?>;

var Point = new Array();

for(var i=0;i<rownum;i++) {
    Point.push(new BMap.Point(longitude[i], latitude[i]));
}

var transPoint = new Array();
var Marker = new Array();

function transcoord() {
    translateCallback = function (data) {
        if (data.status === 0) {
            for(var i=0;i<rownum;i++) {
                transPoint[i]=data.points[i];
                Marker[i]=new BMap.Marker(data.points[i]);
                map.addOverlay(Marker[i]);

            }
        }else{
            //alert(data.status);
        }
    }
    setTimeout(function () {
        var convertor = new BMap.Convertor();
        convertor.translate(Point, 1, 5, translateCallback);
    }, 1000);
}
transcoord();

for(var i=0;i<rownum;i++){

    function ss() {
        var thePoint = transPoint[i];
        var theMarker = Marker[i];
        var theTime = "00:00:00";
        theMarker.addEventListener("click",function(){
            showInfo(this,theTime);
        });
    }
    ss();
}

function showInfo(thisMaker,time){
    var sContent =
        "<ul style='margin:0 0 5px 0;padding:0.2em 0'>"
        +"<li style='line-height: 10px;font-size: 15px;'>"
        +"<span style='width: 50px;display: inline-block;'>000:</span>" + "000" + "</li>"
        +"<li style='line-height: 26px;font-size: 15px;'>"
        +"<span style='width: 50px;display: inline-block;'>time:</span>"+time +"</li>"
        +"</ul>";
    var infoWindow = new BMap.InfoWindow(sContent);  
    thisMaker.openInfoWindow(infoWindow);   
}

大概就是先把一堆GPS座標轉換之後存到數組裡,然後放marker設定一個addEventListener使得點擊marker的時候可以彈出信息,但是運行的時候提示Cannot read property 'addEventListener' of undefined,什麼意思不認識addEventListListenerener這個命令嗎? ...求解

淡淡烟草味淡淡烟草味2775 天前1184

全部回覆(1)我來回復

  • 世界只因有你

    世界只因有你2017-05-16 13:09:21

    addEventListener只適用於標籤元素的事件監聽

    回覆
    0
  • 取消回覆