动态给图片添加点击方法
- HTML code
<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>方法添加成功,点击却没有响应,请问如何解决?
[解决办法]
按理说楼主的做法没有错~~~
如果急用的话,这样做也可以~~
document.getElementById("imga").onclick=function(){alert('a');}
[解决办法]
不是添加的.
- JScript code
function addAttribute(){ document.getElementById("imga").onclick=function() { alert("a"); }}
[解决办法]
- HTML code
<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>