【发布时间】:2013-03-16 11:43:21
【问题描述】:
我正在尝试根据返回的 JSON 更新(替换内容)表格单元格。
如果我使用 console.log 在循环中呈现我的结果 - 我会得到正确/预期的结果。 但是,当我添加 DOM 引用时 - (页面)没有任何反应
如果我在控制台中键入 js 来更改 DOM - 它可以工作...所以我确定语法是正确的...
这是 ajax 调用的一部分,其内容与问题无关 - 因为返回 (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'].update('hey');”,'myKey' 的 heml 元素 id 会更改为 'hey'...
我很困惑。
【问题讨论】:
-
为什么不直接使用
$(key).update('hey');?
标签: javascript dom prototypejs