JS怎么设置对象的onclick事件

var dclose = document.createElement("div");
dclose.style.marginTop = "3px";
dclose.style.marginRight = "6px";
dclose.align = "right"
dclose.innerHTML = "<img id='close' src='../../images/close1.gif' alt='关闭' />";
想给DIV或img加一个onclick事情执行123()函数,想实现鼠标在图片上的时候换成close2.gif图片,请问怎样可以实现?

//给div加点击事件
dclose.onclick=function(){
    
}

还有你确定实现鼠标在图片上的时候换图片不是用onmouseover和onmouseout?

温馨提示:内容为网友见解,仅供参考
第1个回答  2013-07-07
dclose.onclick=function(){document.getElementById("close").src="u1.jpg";}
第2个回答  2013-07-07
dclose.onclick=function(){
if(this.getElementsByTagName("img")[0].src=="../../images/close1.gif"){
this.getElementsByTagName("img")[0].src="../../images/close2.gif";
}
}

是这样嘛?
相似回答