我想通过按钮更改 OpenLayers 图层的可见性(以及其他一些内容)。当我通过 jQuery 在按钮单击事件上调用 anyLayer.setVisibility("true") 时,它正在工作:
$("#anyLayer_button").click(function(){
anyLayer.setVisibility("true");
}
但是当我想使用高级 jQuery 函数对许多层执行此操作并因此使用变量调用 Ol 层时,firebug 告诉我“anyLayer.setVisibility 不是函数”。
function superiorFunction(anylayer) {
$("#" + anyLayer + "_button").click(function(){
anyLayer.setVisibility("true");
otherFunction1(anyLayer);
otherFunction2(anyLayer);
});
}
其他(jQuery)函数在该函数内工作没有任何问题。 如何解决这个问题?
最佳答案
我感觉您将错误的参数传递给了第二个函数:
function superiorFunction(anylayer) { $("#" + anyLayer + "_button").click(function(){ anyLayer.setVisibility("true"); otherFunction1(anyLayer); otherFunction2(anyLayer); }); }
如果您传递的 anylayer
参数是字符串,则 anyLayer.setVisibility("true");
会导致错误,因为字符串没有 setVisibility()
方法...
我认为您真正想要的是一个采用图层名称(用于构造按钮的 id 来注册事件)和图层本身的函数:
function superiorFunction(anyLayer_name, anyLayer) {
$("#" + anyLayer_name + "_button").click(function(){
anyLayer.setVisibility("true");
otherFunction1(anyLayer);
otherFunction2(anyLayer);
});
}
关于javascript - 简单的 OpenLayers 函数在 jQuery 函数中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24329951/