JavaScript DOM操作案例点击图片修改图片路径
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title></title> 6 </head> 7 <body> 8 <a href="images/1.jpg" id="a1"><img src="images/1-small.jpg" alt="" id="im"/></a> 9 <script> 10 //通过id获取图片元素,注册点击事件,添加事件处理函数 11 document.getElementById("im").onclick = function () { 12 //将图片路径设置为a标签的href 13 this.src = document.getElementById("a1").href; 14 //return false; 15 }; 16 </script> 17 </body> 18 </html>