1

我有一个定义如下所示的基本地图(伪代码)。

在 map.addLayers 调用之后,有一个我想从 html 文档的主体调用的 displayData(theid) ,问题是我无法调用该方法。如果我将 onclick 事件绑定到 <a> 标记的 id,我可以实现我所追求的,但是将有太多链接无法绑定到每个 <a> 标记

单击下面的超链接之一会导致未定义方法 displayData()。

谢谢阅读!

  <script>
        Ext.onReady(function() {
        map = new OpenLayers.Map();
        basemap = new OpenLayers.Layer.WMS(...);
        mylines = new OpenLayers.Layer.WMS(...);
        selected_items = new OpenLayers.Layer.Vector(...);
        map.addLayers([basemap, mylines, selected_items]);

        //attach an onclick even for the button on the search results page
        function displayData(theid) {
            OpenLayers.Request.GET({
                url: 'http://1.1.1.1:8080/geoserver/wfs?service=WFS&version=1.0.0&request=GetFeature&featureId='+theid,
                proxy: "../gis/geoserver.seam?url=",
                success: function(response) {
                //if were successful then add the returned features to the map and zoom to the extent
                var gmlReader = new OpenLayers.Format.GML({ extractAttributes: true });
                selected_items.removeAllFeatures();
                selected_items.addFeatures(gmlReader.read(response.responseText));
                map.zoomToExtent(selected_items.getDataExtent());
            }
        });
    }
}
</script>

<body>
    <div id="map" class="smallmap"></div>
    <br/><a href="" onlick="displayData('feature1');">View on map</a>
    <br/><a href="" onlick="displayData('feature2');">View on map</a>
    <br/><a href="" onlick="displayData('feature3');">View on map</a>
    <br/><a href="" onlick="displayData('feature4');">View on map</a>
</body>
4

1 回答 1

1

JavaScript 具有函数范围,这意味着displayData从外部“不可见”

Ext.onReady(function() {
... stuff here is executed but is not visible from outside
}

要解决此问题,您可以在其外部声明一个假的空 displayData 或绑定链接的单击事件(通过类)。

于 2013-10-09T12:47:56.770 回答