动态给图片添加点击方法
<html xmlns="http://www.w3.org/1999/xhtml"><head> <title>动态给图片添加点击方法</title> <script type="text/javascript" language="javascript"> function addAttribute() { document.getElementById("imga").setAttribute("onclick","alert('a')"); } </script></head><body onload="addAttribute();"> <img id="imga" src="http://www.baidu.com/img/sslm1_logo.gif" /></body></html>function addAttribute(){ document.getElementById("imga").onclick=function() { alert("a"); }}
[解决办法]
<html xmlns="http://www.w3.org/1999/xhtml"><head> <title>动态给图片添加点击方法</title> <script type="text/javascript" language="javascript"> function addAttribute() { document.getElementById("imga").setAttribute("onclick",function(){alert('a')}); } </script></head><body onload="addAttribute();"> <img id="imga" src="http://www.baidu.com/img/sslm1_logo.gif"/></body></html>