当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 排他思想

排他思想

2019年11月25日  | 移动技术网IT编程  | 我要评论
排他思想:
   点击其中一个时其他的变,就自己不变 如图:
 

 


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'; } } } }

  

 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网