zl程序教程

您现在的位置是:首页 >  其他

当前栏目

事件代理 方式百度换皮肤详解编程语言

事件代理百度编程语言 详解 方式 皮肤
2023-06-13 09:20:36 时间

!DOCTYPE html
html
head lang= en
meta charset= UTF-8
title /title
style
*{margin:0;padding:0}
body {

background: url(images/1.jpg) top center;
}
.box {
height: 150px;
background: rgba(255,255,255,.3);
text-align: center;
padding-top:50px;
}
.box img {
cursor: pointer;
}
/style
script
window.onload = function(){
/*通过事件代理来解决问题*/
var box=document.querySelector( .box );
box.addEventListener( click ,function(e){

if(e.target.tagName.toLowerCase()== img ){
//获得图片src 需要解析
//解析之后是file:///E:/JS学习/04. 京东商城项目实战(共60多集)/2.京东商城-JS/第1天/笔记源码/案例/08 百度换肤/images/1.jpg

var imgUrl=decodeURIComponent(e.target.src);
//图片放在哪个文件夹就需要找到文件夹字符串的位置 来找到图片的相对位置
var sign=imgUrl.lastIndexOf( images );
//截取需要的路径
var url=imgUrl.slice(sign);
document.body.style.backgroundImage= url( +url+ )
}

},false);
}
/script
/head
body
div > img src= images/1.jpg alt= width= 150 id= pic1 /
img src= images/2.jpg alt= width= 150 id= pic2 /
img src= images/3.jpg alt= width= 150 id= pic3 /
/div
/body
/html

 

事件代理 方式百度换皮肤详解编程语言

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/12285.html

c