日期:2014-05-16 浏览次数:20442 次
<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>