从数组字符串动态分配给document.getElementById

时间:2011-07-21 05:09:03

标签: javascript

var urlName=new Array("total","pub");
var query = window.location.search.substring(1);

for (var i=0;i<vars.length;i++) {
    document.getElementById("'"+urlName[i]+"'").innerHTML = pair[i];
}

如何动态分配UrlNames?

1 个答案:

答案 0 :(得分:1)

您不需要urlName[i]周围的引号,因为它是一个字符串:

var urlName = ["total","pub"];

for (var i=0;i<urlName.length;i++) {
    document.getElementById(urlName[i]).innerHTML = pair[i];
}