如果我想使用innerHTML向元素添加内容,那么正确的语法是什么。 以下是我的非工作示例:
openNewWindow: function(content) {
popupWin = window.open(content,
'open_window',
'menubar, toolbar, location, directories, status, scrollbars, resizable, dependent, width=640, height=480, left=0, top=0')
},
for (var index in mv.exifImages) {
ele.innerHTML += "<p onclick = openNewWindow(mv.exifImages[index]> image" + index + "</p>";
}
答案 0 :(得分:1)
我认为是。变量index
具有局部范围
for (var index in mv.exifImages) {
ele.innerHTML += "<p onclick = 'openNewWindow(\"" + mv.exifImages[index] + "\")'> image" + index + "</p>";
}
答案 1 :(得分:0)
使用appendChild
:
var myelement = document.getElementById("myelement");
myelement.appendChild( document.createTextNode("Example text inside myelemen") );
这比覆盖innerHTML更好,因为它保留了onclick事件,例如:
Is it possible to append to innerHTML without destroying descendants' event listeners?
答案 2 :(得分:0)
innerHTML不是你的问题,你的代码缺乏上下文。
openNewWindow: function(content) {
popupWin = window.open(content,
'open_window',
'menubar, toolbar, location, directories, status, scrollbars, resizable, dependent, width=640, height=480, left=0, top=0');
// call to window.open ended here
},
// judging by the definition of openNewWindow and the comma above,
// we are inside an object definition!
// you cannot embed a for loop inside an object definition!!!
for (var index in mv.exifImages) {
ele.innerHTML += "<p onclick = openNewWindow(mv.exifImages[index]> image" + index + "</p>";
}
将你的for循环放在合理的地方,比如在函数内部,或实际向我们显示你得到的错误。