国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

淺談JavaScript_DOM學習篇_圖片切換小案例

2024-05-06 15:41:36
字體:
來源:轉載
供稿:網友

今天開始學習DOM操作,下面寫一個小案例來鞏固下知識點.

DOM: document object model (文檔對象模型)

根據id獲取頁面元素 : 如: var xx = document.getElementById("id");

根據標簽獲取元素: 如: var xx = document.getElementsByTagName("div");

<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>Title</title>  <style>    * {      margin: 0;      padding: 0;    }    #outer {      width: 500px;      /*設置上邊距50px 水平居中*/      margin: 50px auto;      /*設置邊框*/      padding: 10px;      background-color: greenyellow;      /*設置文本居中*/      text-align: center;    }    img {      width: 500px;    }  </style>  <script>     //btn 為按鈕id  clickEventFunction 為點擊后執行的操作函數    function addClick(btn, clickEventFunction) {      var myButton = document.getElementById(btn);      myButton.onclick = clickEventFunction;    };    window.onload = function () {      (function () {        var pics = ["imgs/1.png",          "imgs/2.png",          "imgs/3.png"];        var index = 0;        showPicNum(index);        var img = document.getElementsByTagName("img")[0];        // var btn1 = document.getElementById("btn1");        var btn2 = document.getElementById("btn2");        addClick("btn1", function () {          index--;          if (index <= -1) {            index = pics.length - 1;          }          console.log(index + " ----- ");          img.src = pics[index];          showPicNum(index);        });        addClick("btn2", function () {          index++;          if (index >= pics.length) {            index = 0;          }          console.log(index + " ++++++++ ");          img.src = pics[index];          showPicNum(index);        });        //        // btn1.onclick = function () {        //   index --;        //   if(index <= -1){        //     index = pics.length - 1;        //   }        //   console.log(index + " ----- ");        //   img.src = pics[index];        //   showPicNum(index);        // };        // btn2.onclick = function () {        //   index ++;        //   if(index >= pics.length){        //     index = 0;        //   }        //   console.log(index + " ++++++++ ");        //   img.src = pics[index];        //   showPicNum(index);        // };        console.log(index);        /**         * 展示當前圖片為第幾張         * @param index  當前圖片索引         */        function showPicNum(index) {          var descrs = document.getElementById("discs");          descrs.innerText = "一共" + pics.length + "張圖片,當前第" + ++index + "張";        }      }())    };  </script></head><body><div id="outer">  <p id="discs"></p>  <img src="imgs/1.png"/><br>  <button id="btn1" type="button" value="上一張">上一張</button>  <button id="btn2" type="button" value="下一張">下一張</button></div></body></html>            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 松原市| 龙岩市| 台州市| 滁州市| 南和县| 梁平县| 洪泽县| 临桂县| 丰原市| 曲阳县| 赞皇县| 元氏县| 海原县| 施秉县| 宁城县| 莎车县| 济源市| 诸城市| 泽州县| 蓬莱市| 镇坪县| 山东省| 涞水县| 乌拉特中旗| 湖南省| 镇沅| 二连浩特市| 丘北县| 屯昌县| 灵川县| 荥阳市| 枣阳市| 民县| 九龙县| 巴马| 高雄市| 潮安县| 沧源| 高邮市| 赤峰市| 霍邱县|