[英]Image element not placed properly inside div
I try to place an img into a div, at a specific place (200, 100) but it always gets positioned in the top left corner (0, 0). 我尝试将img放置到div的特定位置(200、100),但它始终位于左上角(0、0)。 What am I doing wrong? 我究竟做错了什么?
<!doctype html>
<html>
<head>
<title>Matching Game</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body id="theBody" onload="build_set()">
<h1>Matching Game</h1>
<div id="leftSide" style="position:absolute; top:100px; left:0; width:500px; height:500px"></div>
<div id="rightside" style="position:absolute; top:100px; left:500px; width:500px; height:500px; border-left:3px solid black"></div>
<script>
function build_set() {
var leftSide = document.getElementById("leftSide");
var rightSide = document.getElementById("rightSide");
var smile = document.createElement("img");
smile.setAttribute("src", "http://home.cse.ust.hk/~rossiter/mooc/matching_game/smile.png")
smile.style.top = "200px";
smile.style.left = "100px";
leftSide.appendChild(smile);
}
</script>
</body>
</html>
You need to set position
for img also. 您还需要设置img的position
。 Here's jsFiddle 这是jsFiddle
function build_set() { var leftSide = document.getElementById("leftSide"); var rightSide = document.getElementById("rightSide"); var smile = document.createElement("img"); smile.setAttribute("src", "http://home.cse.ust.hk/~rossiter/mooc/matching_game/smile.png") smile.style.top = "200px"; smile.style.left = "100px"; smile.style.position = "absolute"; leftSide.appendChild(smile); } build_set();
<h1>Matching Game</h1> <div id="leftSide" style="position:absolute; top:100px; left:0; width:500px; height:500px"></div> <div id="rightside" style="position:absolute; top:100px; left:500px; width:500px; height:500px; border-left:3px solid black"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.