调用具有多个值的函数
Call function with many values
如何在一种模式下调用所有选项?示例:hide("house, building, clothes") 或 hide("house","building","clothes")... 可能吗?
// == 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");
谢谢大家:)
使用参数对象
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");
您可以使用 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
的说明
如何在一种模式下调用所有选项?示例:hide("house, building, clothes") 或 hide("house","building","clothes")... 可能吗?
// == 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");
谢谢大家:)
使用参数对象
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");
您可以使用 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
的说明