document.getElementById(array [x])返回null

时间:2015-06-29 22:20:23

标签: javascript

我试图使用数组索引值调用一个元素,但我一直收到一条空消息。

JS:

function img_disp() {
  var num = 0;
  var images_array = ["person_1","person_2","person_3","person_4","person_5","person_6","person_7","person_8","person_9","person_10","person_11","person_12"];
  document.getElementById(images_array[num]).style.visibility = "visible";
}

我可以做些什么来完成这项工作?我希望能够使用1个函数调用不同的ID。

由于

1 个答案:

答案 0 :(得分:0)

您没有递增变量 [{"category_id":"1","category_name":"Academic Analysis"}, {"category_id":"2","category_name":"Teaching Analysis"},{"category_id":"3","category_name":"Skill Analysis"}] ,您可以使用loops来执行此操作,例如:

num

function img_disp() { var images_array = ["person_1","person_2","person_3","person_4","person_5","person_6","person_7","person_8","person_9","person_10","person_11","person_12"]; for(var num = 0; num < images_array.length; num++){ document.getElementById(images_array[num]).style.visibility = "visible"; } } 应该在加载DOM时调用它。请查看此question,了解有关它的更多信息。