代码如何,谢谢各位了。
var img = document.createElement("img");
img.style.margin = "0.8em 0";
img.setAttribute("src", "img/20.gif");
document.getElementById("msgDiv").appendChild(img);
var img = document.createElement("img");
img.style.margin = "0.8em 0";
img.setAttribute("src", "img/20.gif");
document.getElementById("msgDiv").appendChild(img);
img.style.margin = "10px 0";
img.setAttribute("src", "img/20.gif");
document.getElementById("msgDiv").appendChild(img);
试下,是不是0.8em的问题
比方你的html文件路径为:c:\workspac\test\demo.html 你直接双击(或者在地址栏输入此路径),在chrome里不能直接访问本地文件的(img/20.gif),必须是http路径才行。
用绝对地址试试~ http://开头的这种~
<html>
<head>
</head>
<body>
<div id='msgDiv'></div>
<script>
var img = document.createElement("img");
img.style.margin = "0.8em 0";
img.setAttribute("src", "img/20.gif");
document.getElementById("msgDiv").appendChild(img);
</script>
</body>
</html>
上面的代码在 IE8 和 Chrome 23 上面都可以运行。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function init(){
var img = document.createElement("img");
img.style.margin = "0.8em 0";
img.setAttribute("src", "1.jpg");
document.getElementById("msgDiv").appendChild(img);
}
window.onload=init;
</script>
</head><body>
<div id="msgDiv"></div>
</body>
</html>
mei wen ti a