[英]Created image element is not placed at the correct location inside a div
我有一个包含一些div元素的HTML,例如:
<body id="theBody">
<div id="box1">
<div id="boxLeft"></div>
<div id="boxRight"></div>
</div>
...
在JavaScript代码中,我做这样的事情:
var theLeftSide = document.getElementById("boxLeft");
newImg = document.createElement("img");
newImg.src = "image1.png";
topPos = 200;
leftPos = 200;
newImg.style.top = topPos + "px";
newImg.style.left = leftPos + "px";
newImg.setAttribute("width", "60px");
theLeftSide.appendChild(newImg);
我希望该图像显示在boxLeft
内的(200,200)的正确位置,但是它显示在页面的左上角。
我在这里做错了什么?
我对此表示感谢。
尝试添加此CSS:
#boxLeft{
position: relative;
}
img{
position: absolute;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.