为什么我的Javascript不能在innerHTML上工作
innerHTML应该具有img标记的id为什么我的Javascript不能在innerHTML上工作,javascript,html,Javascript,Html,innerHTML应该具有img标记的id count = count + 1; block_to_insert = document.createElement( 'div' + count); block_to_insert.innerHTML = '<div width="980px" height="40px" style="background-color:black"><img src="" id="preview" width="150px" height="
count = count + 1;
block_to_insert = document.createElement( 'div' + count);
block_to_insert.innerHTML = '<div width="980px" height="40px" style="background-color:black"><img src="" id="preview" width="150px" height="150px"></img><font color="white">template2</font></div>';
containerbody.appendChild(block_to_insert);
而且它不起作用。当我把img标记放在HTML中时,它就起作用了。什么是计数
block_to_insert = document.createElement( 'div' + count);
这很可能是导致问题的原因
count
不是定义的变量
如果之前已经定义过,也许我们可以看看它?
没有上下文就无法提供良好的帮助请尝试以下方法:
count = count + 1;
block_to_insert = document.createElement( 'div' + count);
block_to_insert.id = "preview";
block_to_insert.innerHTML = '<div width="980px" height="40px" style="background-color:black"><img src="" width="150px" height="150px"></img><font color="white">template2</font></div>';
containerbody.appendChild(block_to_insert);
请注意,我的目标是
img
的父对象,而不是img
本身。这是因为innerHTML
中的代码没有注册到DOM。然后,您可以使用createElement
count=count+1以创建的元素的子节点为目标
@tylerschouenau-是否在任何地方定义了count
?如果不是,则count=count+1
将生成一个错误,即count=count+1代码>
count = count + 1;
block_to_insert = document.createElement( 'div' + count);
block_to_insert.id = "preview";
block_to_insert.innerHTML = '<div width="980px" height="40px" style="background-color:black"><img src="" width="150px" height="150px"></img><font color="white">template2</font></div>';
containerbody.appendChild(block_to_insert);
var preview = document.getElementById("preview");