45 图片切换练习 发表于 2021-08-26 分类于 JavaScript学习之路 阅读次数: 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> #outer{ text-align: center; margin: 0px auto; width: 500px; padding: 5px; background-color: beige; } #prev,#next{ margin-top: 5px; padding: 10px; width: 130px; background-color: rgb(118, 94, 255); border-radius :10px; color: honeydew; font-size:15px; } img{ width:479px; height:478px; } </style></head><body> <div id = "outer"> <p id = info>共n张图片,这是第m张</p> <img id = "img" src = "C:/Users/Dell/Desktop/前 端/resource/xiaotupian (1).jpg" alt = "tupian"> <button id = "prev"><span id = "asd">上一页</span></button> <button id = "next"><span>下一页</span></button> </div> <script> var prev = document.getElementById("prev"); var next = document.getElementById("next"); var img = document.getElementById("img"); var info = document.getElementById("info"); var index = 0; var imgArr = [ "C:/Users/Dell/Desktop/前端/resource/xiaotupian (1).jpg", "C:/Users/Dell/Desktop/前端/resource/xiaotupian (2).jpg", "C:/Users/Dell/Desktop/前端/resource/xiaotupian (3).jpg", ]; info.innerHTML = "共有"+imgArr.length+"张图片,这是第"+(index+1)+"张"; prev.onclick = function(){ if(index > 0){ index--; img.src = imgArr[index]; info.innerHTML = "共有"+imgArr.length+"张图片,这是第"+(index+1)+"张"; } } next.onclick = function(){ if(index < imgArr.length-1){ index++; img.src = imgArr[index]; info.innerHTML = "共有"+imgArr.length+"张图片,这是第"+(index+1)+"张"; } } </script></body></html> -------------本文结束感谢您的阅读------------- 打赏 微信支付 支付宝 本文作者: Sun Aoxin 本文链接: https://qaaa.top/2021/08/26/JavaScript学习记录45/ 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!