具有许多值的调用函数

时间:2015-11-21 18:16:31

标签: javascript function google-maps hide show

如何仅在一种模式下调用所有选项?例如:隐藏("房子,建筑,衣服")或隐藏("房子","建筑","衣服")......有可能吗?

// == shows all markers of a particular category and ensures the checkbox is checked
function show(category) {
    for (var i=0; i<locations.length; i++) {
        if (locations[i][2] == category) {
            markers[i].setVisible(true);
        }
    }
}
// hides all markers of a particular category and ensures the checkbox is cleared
function hide(category) {
    for (var i=0; i<locations.length; i++) {
        if (locations[i][2] == category) {
            markers[i].setVisible(false);
        }
    }
}
// show or hide the categories initially
hide("house");
hide("building");
hide("clothes");

全部谢谢:)

2 个答案:

答案 0 :(得分:1)

您可以使用javascript对象:

toggleShowHide({"house":true,"building":false,"clothes":true});
function toggleShowHide(options) {
  for(key in options)
  {
    if (options.hasOwnProperty(key)) {
      for (var i=0; i<locations.length; i++) {
          if (locations[i][2] == key) {
             markers[i].setVisible(options[key]);
          }
        }
      }
  }

}

请参阅hasOwnProperty

的说明

答案 1 :(得分:0)

使用参数对象

function show() {
    for(var j = 0; j<arguments.length; j++){
      for (var i=0; i<locations.length; i++) {
        if (locations[i][2] == arguments[j]) {
            markers[i].setVisible(true);
        }
      }
    }
}
// hides all markers of a particular category and ensures the checkbox is cleared
function hide() {
    for(var j = 0; j<arguments.length; j++){
      for (var i=0; i<locations.length; i++) {
        if (locations[i][2] == arguments[j]) {
            markers[i].setVisible(false);
        }
      }
}
// show or hide the categories initially
hide("house", "building", "clothes");