abstrait:<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>jq獲取隨機(jī)顏色</title> <script src="jquery-3.3.1.min.js"></script&
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>jq獲取隨機(jī)顏色</title> <script src="jquery-3.3.1.min.js"></script> <style> a{ float: left; margin: 50px; width: 100px; display: block; line-height: 100px; color: #000; text-align: center; height: 100px; border-radius: 50px; text-decoration: none; } </style> <script> function bg (tag) { // body... var len = document.getElementsByTagName(tag).length //獲取長度。 for (var i = 0; i<len ; i++) { // 隨機(jī)rgb寫法 Math.floor(Math.random()*256) document.getElementsByTagName(tag)[i].style.backgroundColor='rgb('+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+')' //你少了一個拼接符 “+”//length返回數(shù)組中元素的數(shù)目 } } $(document).ready(function(){ bg('a') $('a').mouseover(function(){ $bgc=$(this).css('backgroundColor') $(this).css('box-shadow','0px 0px 20px'+$bgc) $(this).css('border-radius','20px') }) $('a').mouseleave(function(){ $(this).css('box-shadow','none') $(this).css('border-radius','50px') }) }) </script> </head> <body> <a href="#">1</a> <a href="#">2</a> <a href="#">3</a> <a href="#">4</a> </body> </html>
獲取標(biāo)簽名數(shù)組:getElementsByTagName
隨機(jī)rgb寫法 Math.floor(Math.random()*256)
總結(jié):通過獲取標(biāo)簽名數(shù)組,來獲取a標(biāo)簽的長度。再根據(jù)長度使用for循環(huán)遍歷就能同時修改所有a標(biāo)簽參數(shù)。再使用jq的鼠標(biāo)移上 移出事件改變css。這個案例就完成了
Professeur correcteur:天蓬老師Temps de correction:2018-11-09 21:40:46
Résumé du professeur:你的思路是正確的,仔細(xì)想一下,是否還有更簡便的實現(xiàn)方法呢?