JavaScript_DOMѧϰƪ_ͼƬÇл»Ð¡°¸Àý

2018-07-22 06:06:51À´Ô´£º²©¿ÍÔ° ÔĶÁ ()

ÐÂÀÏ¿Í»§´ó»ØÀ¡,ÔÆ·þÎñÆ÷µÍÖÁ5ÕÛ

½ñÌ쿪ʼѧϰDOM²Ù×÷,ÏÂÃæдһ¸öС°¸ÀýÀ´¹®¹ÌÏÂ֪ʶµã.

DOM: document object model (Îĵµ¶ÔÏóÄ£ÐÍ)

¸ù¾Ýid»ñÈ¡Ò³ÃæÔªËØ :¡¡Èç:¡¡var xx = document.getElementById("id");

¸ù¾Ý±êÇ©»ñÈ¡ÔªËØ:¡¡Èç:¡¡var xx = document.getElementsByTagName("div");

src="imgs/1.png"/><br>    <button id="btn1" type="button" value="
<!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
Ϊµã»÷ºóÖ´ÐеIJÙ×÷º¯Êý 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>

¡¡¡¡ÎĵµÄ¿Â¼:¡¡¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡  

¡¡¡¡Ð§¹ûÈçÏÂ:

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡

 

ÉÏÒ»ÕÅ">ÉÏÒ»ÕÅ</button>    <button id="btn2" type="button" value="ÏÂÒ»ÕÅ">ÏÂÒ»ÕÅ</button></div></body></html>

 

±êÇ©£º

°æȨÉêÃ÷£º±¾Õ¾ÎÄÕ²¿·Ö×ÔÍøÂ磬ÈçÓÐÇÖȨ£¬ÇëÁªÏµ£ºwest999com@outlook.com
Ìرð×¢Ò⣺±¾Õ¾ËùÓÐתÔØÎÄÕÂÑÔÂÛ²»´ú±í±¾Õ¾¹Ûµã£¬±¾Õ¾ËùÌṩµÄÉãÓ°ÕÕƬ£¬²å»­£¬Éè¼Æ×÷Æ·£¬ÈçÐèʹÓã¬ÇëÓëÔ­×÷ÕßÁªÏµ£¬°æȨ¹éÔ­×÷ÕßËùÓÐ

ÉÏһƪ£º°Ù¶ÈµØͼ¹öÂÖËõ·Åʱ²úÉúλÖÃÆ«ÒÆ ÎÊÌâ

ÏÂһƪ£ºcanvas »­Ê±ÖÓ »á¶¯ßÏ