前言:本人是新生小白,JavaScript学的很菜,如有错误之处欢迎指出。
目录
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title></title>
- </head>
- <body>
- </body>
- </html>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title></title>
- <style>
- img{
- width: 150px;
- height: 150px;
- }
-
- body{
- background-image: url(img/1.png);
- background-size: cover;
- background-repeat: no-repeat;
- }
- </style>
- </head>
- <body>
- <img src="./img/1.png" alt="">
- <img src="./img/2.png" alt="">
- <img src="./img/3.png" alt="">
-
-
- </body>
- </html>
-
-
-
- <script>
- var imgs = document.querySelectorAll('img')
-
-
- </script>
-
-
-
-
-
-
-
-
-
- <script>
- var imgs = document.querySelectorAll('img')
-
- for(var i=0;i<imgs.length;i++){
- imgs[i].onclick = function(){
-
- }
- }
- </script>
-
-
-
-
- <script>
- var imgs = document.querySelectorAll('img')
-
- for(var i=0;i<imgs.length;i++){
- imgs[i].onclick = function(){
- var src = this.src
-
-
- }
- }
- </script>
-
-
-
- <script>
- var imgs = document.querySelectorAll('img')
-
- for(var i=0;i<imgs.length;i++){
- imgs[i].onclick = function(){
- var src = this.src
- // console.log(src)
- document.body.style.backgroundImage = 'url('+src + ')'
- }
- }
- </script>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title></title>
- <style>
- img{
- width: 150px;
- height: 150px;
- }
-
- body{
- background-image: url(img/1.png);
- background-size: cover;
- background-repeat: no-repeat;
- }
- </style>
- </head>
- <body>
- <img src="./img/1.png" alt="">
- <img src="./img/2.png" alt="">
- <img src="./img/3.png" alt="">
-
-
- <script>
- var imgs = document.querySelectorAll('img')
-
- for(var i=0;i<imgs.length;i++){
- imgs[i].onclick = function(){
- var src = this.src
- // console.log(src)
- document.body.style.backgroundImage = 'url('+src + ')'
- }
- }
- </script>
- </body>
- </html>
小提示:记得提前给图片设置好大小,防止图片大小不一出现布局难看。