如何使用Javascript将此代码注入HTML?

如何使用Javascript将此代码注入HTML?,javascript,html,css,Javascript,Html,Css,var div=document.createElementdiv; div.setAttributeid,bar; div.style.position=固定; div.style.top=0; div.style.left=0; div.style.height=50px; div.style.width=50px; div.style.backgroundColor=ccc; document.body.appendChilddiv;将HTML代码片段附加到网页正文中的问题到底是什么?你试

var div=document.createElementdiv; div.setAttributeid,bar; div.style.position=固定; div.style.top=0; div.style.left=0; div.style.height=50px; div.style.width=50px; div.style.backgroundColor=ccc;
document.body.appendChilddiv;将HTML代码片段附加到网页正文中的问题到底是什么?你试过什么?您收到的错误消息是什么,或者您的方法有什么问题?更新了问题并单独分配id。document.createElementdiv.id=bar返回字符串bar@ThumChoonTat也试过了div.id=bar;没有工作var.div和div.bar在这里没有任何意义。只要使用div。
<div id="bar" style="position:fixed; top:0; left:0; width: 0%; height: 10px; background-color:#e81d61;"></div> 
var div = document.createElement("div").id="bar";
div.bar.style.position = "fixed";
div.bar.style.top = "0";
div.bar.style.left = "0";
div.bar.style.height = "10px";


document.body.appendChild(div);