简体   繁体   English

无法从另一个文件中 getElementById

[英]Can't getElementById from another file

I have set up an leaflet map in the index.php and here I include a external *.js file, which connect to an API service to recieve real time data.我在 index.php 中设置了一个传单地图,在这里我包含一个外部 *.js 文件,该文件连接到 API 服务以接收实时数据。 the received data must be processed and that happens in the external * .js file, which is included in the header of the index.php必须处理接收到的数据,这发生在外部 * .js 文件中,该文件包含在 index.php 的标头中

the processed data will be shown as refreshing markers on the map and know my goal is to jump to marker1 by clicking button1.处理后的数据将在地图上显示为刷新标记,并且知道我的目标是通过单击按钮 1 跳转到标记 1。 to jump to the markers is inspired by this fiddle :跳到标记的灵感来自这个小提琴

var markersById = {}
var TTvehicleObj = {};

function refreshTable(){

   // code which creates TTvehicleObj

    function addTTlayer() {
        TTJsonLayer = L.geoJSON(null, {
            pointToLayer: function (feature, latlng) {
                return L.marker(latlng, {icon: vehicleMarker});
            },
            onEachFeature: function(feature, layer) {
                if (feature.properties.Ignition == 0 && feature.properties.Standstill == 1) {
                        vehicleParking = '<span class="label label-default">P</span>&nbsp;';
                    } else if (feature.properties.Ignition == 1 && feature.properties.Standstill == 1) {
                        vehicleParking = '<span class="label label-primary">P</span>&nbsp;';
                    } else {
                        vehicleParking = '';
                    }
                    layer.bindPopup(vehicleParking+'<strong>'+feature.properties.Funknummer+'</strong>&nbsp;|&nbsp;'+feature.properties.Position);

                    if (feature.properties.id) {
                        markersById[feature.properties.id] = layer;
                    }
                }
            });

        TTJsonLayer.addData(TTvehicleObj);
        TTJsonLayer.addTo(map);
        //map.fitBounds(TTJsonLayer.getBounds());

        $.each(TTvehicleObj.features, function(key, value) {
            //console.log(key+':'+value.properties.id);
            setMarkerButton('"vehicle'+value.id+'"');
        });         

        function setMarkerButton(id) { 
            document.getElementById(id).addEventListener("click", function () {
            map.setView(markersById[id].getLatLng(), map.getZoom());
          });
        }
    }

    function refreshTTlayer() {
        if (typeof TTJsonLayer === 'undefined') {
            console.log(consoleLogTime.getHours()+':'+consoleLogTime.getMinutes()+':'+consoleLogTime.getSeconds()+' TTlayer ist nicht vorhanden');
            addTTlayer();
            console.log(consoleLogTime.getHours()+':'+consoleLogTime.getMinutes()+':'+consoleLogTime.getSeconds()+' TTlayer wurde erstellt');
        } else {
            map.removeLayer(TTJsonLayer);
            console.log(consoleLogTime.getHours()+':'+consoleLogTime.getMinutes()+':'+consoleLogTime.getSeconds()+' TTlayer wurde entfernt');
            addTTlayer();
            console.log(consoleLogTime.getHours()+':'+consoleLogTime.getMinutes()+':'+consoleLogTime.getSeconds()+' TTlayer wurde aktualisiert');
        }

    }

    refreshTTlayer();

});
}

now I get the following error:现在我收到以下错误:

Uncaught TypeError: Cannot read property 'addEventListener' of null未捕获的类型错误:无法读取 null 的属性“addEventListener”

theoretical I can explain the error to me, but I don't know how to solve this.理论上我可以向我解释错误,但我不知道如何解决这个问题。

if I try to move the function setMarkerButton in the index.php after refreshTable I will get tis error:如果我尝试在refreshTable之后移动 index.php 中的function setMarkerButton ,我将收到错误消息:

Uncaught ReferenceError: setMarkerButton is not defined未捕获的 ReferenceError:未定义 setMarkerButton

I think i can also explain this error to myself but I am very confused at the moment, therefore I have to ask for help.我想我也可以向自己解释这个错误,但我现在很困惑,因此我不得不寻求帮助。

thx in advance提前谢谢

--- edit --- - - 编辑 - -

the refreshTable is called in the index.php after the included *.js file.在包含 *.js 文件之后,在 index.php 中调用refreshTable i think one of my problem is, that the timing isn't correct and the TTvehicleObj and other functions aren't global available.我认为我的问题之一是,时间不正确,并且TTvehicleObj和其他功能不是全局可用的。

I can rule out mismatched our double used ids.我可以排除我们双重使用的 ID 不匹配的情况。

my question is, how can i acces the looped setMarker();我的问题是,我如何访问循环的setMarker(); from my index.php?从我的 index.php?

As commented, your question lacks details to make it crystal clear what your exact issue is.正如所评论的,您的问题缺乏细节,无法清楚地说明您的确切问题是什么。 In particular, the HTML with <button> 's your trying to reach with document.getElementById() .特别是,带有<button>的 HTML 是您尝试使用document.getElementById()

That being said, I highly suspect that the id you try to compose in:话虽如此,我非常怀疑您尝试编写的id

setMarkerButton('"vehicle'+value.id+'"')

…is incorrect. …是不正确的。

If this ID is incorrect, document.getElementById(id) returns null , on which you cannot call addEventListener , hence your Error message.如果此 ID 不正确,则document.getElementById(id)返回null ,您无法在其上调用addEventListener ,因此您的错误消息。

  1. There is a very high chance you have extra double quotes ( " ).您很有可能有额外的双引号 ( " )。

  2. The value.id should probably have been value.properties.id . value.id应该是value.properties.id Indeed, you are looping through a GeoJSON Feature Collection ( TTvehicleObj.features ).实际上,您正在遍历 GeoJSON 特征集合 ( TTvehicleObj.features )。 Each Feature can indeed have an id (as per GeoJSON standard), but in your case you are accessing a few lines above feature.properties.id , therefore it looks like your id is actually a sub-member of properties .每个 Feature 确实可以有一个id (根据 GeoJSON 标准),但在您的情况下,您正在访问feature.properties.id上方的几行,因此看起来您的id实际上是properties的子成员。

Therefore you would probably need to replace the above line by:因此,您可能需要通过以下方式替换上述行:

setMarkerButton('vehicle'+value.properties.id);

and the <button> (or the actual tag that you use) should have an id that looks like: id="vehicle<ID>" where <ID> is the exact value in feature.properties.id .并且<button> (或您使用的实际标签)应该有一个类似于: id="vehicle<ID>"id ,其中<ID>feature.properties.id的确切值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM