js生成img的三种方式

来源:互联网 发布:.手机域名注册局 编辑:程序博客网 时间:2024/06/16 08:47
<div id="d1"></div>


<script>

//HTML
function a(){
document.getElementById("d1").innerHTML="<img src='1.jpg'>";
}
a();
//方法
function b(){
var d1=document.getElementById("d1");
var img=document.createElement("img");
img.src="2.jpg";
d1.appendChild(img);
}
b();
//对象
function c(){

var cc=new Image();

//cc.onload=function(){alert("img is loaded")}; 
//cc.onerror=function(){alert("error!")};
//cc.src="http://www.abaonet.com/img.gif";
//function show(){alert("body is loaded");};
//window.onload=show;

cc.src="3.jpg";
document.getElementById("d1").appendChild(cc);
}
c();
</script>
原创粉丝点击