zoukankan      html  css  js  c++  java
  • 排他思想

    排他思想:
       点击其中一个时其他的变,就自己不变 如图:
     

    html和css代码

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Title</title>
     6 </head>
     7 <body>
     8 <img src="./images/yellow.png" alt="">
     9 <img src="./images/yellow.png" alt="">
    10 <img src="./images/yellow.png" alt="">
    11 <img src="./images/yellow.png" alt="">
    12 <img src="./images/yellow.png" alt="">
    13 </body>
    14 </html>

    效果图

    js代码

    // 排他思想:
        //      第一种
        //          先把所有的都改变,在单独改变自己本身
        //      第二种
        //          遍历时判断是不是点击的那个,不是就变一样的,是就不变
        //          获取img对象节点
        let imgs = document.querySelectorAll('img');
    	//  第一种:
        for (let i = 0 ;i<imgs.length;i++){
        	imgs[i].onclick = function () {
                for (let i = 0;i<imgs.length;i++){
                	imgs[i].src = './images/green.png';
                }
                this. src = './images/yellow.png';
    		}
        }
        //  第二种: for (let i = 0 ;i<imgs.length;i++){ imgs[i].onclick = function () { for (let j = 0;j<imgs.length;j++){ if (imgs[j] == this){ this. src = './images/yellow.png'; } else { imgs[j].src = './images/green.png'; } } } }

      

     

  • 相关阅读:
    019. Remove Nth Node From End of List
    021.Merge Two Sorted Lists
    自定义starter
    servlet里面转发与重定向
    贪婪模式与非贪婪模式
    localstack 线程隔离
    Algorithm & Data structure
    some interview question
    阿里-菜鸟国际-出口大团队招新啦
    JDK8漫谈——集合更强大
  • 原文地址:https://www.cnblogs.com/lifenghe/p/11927704.html
Copyright © 2011-2022 走看看