原型和dom没有达到预期的结果

时间:2013-03-26 15:36:06

标签: javascript dom prototypejs

我尝试根据返回的JSON更新(替换内容)表格单元格。

如果我使用console.log在循环中渲染结果 - 我得到正确/预期的结果。 但是当我添加DOM引用时 - 没有任何反应(到页面)

如果我输入js来改变控制台中的DOM - 它可以工作......所以我确定语法是正确的......

这是ajax调用的一部分,其中的内容与问题无关 - 因为return(response.responseJSON)是正确的。

onSuccess: function( response ) {
var p = response.responseJSON;
for ( var key in p ) {
    if ( p.hasOwnProperty( key ) ) {
    console.log( key + " = " + p[key] ); // this works, loops correct number and shows key/value as expected
    document.getElementById[key].update('hey'); // if i add this the loop doesn't go past the first key/value - and the page element that matches the key does not change
    }
}
}

如果我输入"的document.getElementById ['的myKey&#39]。更新('哎&#39); "在控制台中,' myKey'的heml元素id是否改为'嘿嘿......

我很困惑。

1 个答案:

答案 0 :(得分:2)

document.getElementById(key)

Parens而不是方括号

var e = document.createElement("DIV");
e.id = "test123123";
document.body.appendChild(e);
console.log(document.getElementById["test123123"]); // undefined
console.log(document.getElementById("test123123")); // actual DOM element