您现在的位置是:首页 > 电脑技术查询 > web开发

动态给图片添加点击方法解决方法

编辑:chaxungu时间:2022-10-02 23:17:58分类:web开发

动态给图片添加点击方法
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 codefunction 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>