Blogger Information
Blog 45
fans 0
comment 1
visits 33099
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
单击事件做个替换图片
源逸
Original
816 people have browsed it

使用循环给每个a标签添加事件,单击选项卡的时候切换图片


实例

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>使用事件给a标签添加的单击事件(结合循环遍历给a标签添加事件,达到点击就切换的效果)2019.05.08</title>
	<style>
.box {
			width: 500px;
			height: 700px;
			background-color: #efefef;
			border: 1px solid lightgray;
			margin: 20px auto;
			text-align: center;
			color: #636363;
			box-shadow: 2px 2px 2px #999;
		}
		.box ul {
			margin:0;
			padding:0;
			/*将ul转为BFC独立块,使之不受内部浮动元素的影响*/
			overflow: hidden;
		}
		.box ul li {
			list-style-type: none;
			float:left;
			background-color: skyblue;
			margin-left: 20px;

		}
		.active {
            font-size:1.2em;
            background-color: coral;
        }
		.box ul li a {
			/*将a转为块元素,并设置为li的宽高,这样可以使整个li可以被点击*/
			display: block;
			width: 100px;
			height: 40px;
			line-height: 40px;
			color: white;
			text-decoration: none;
		}
		.box ul li:hover {
			font-size:1.2em;
			background-color: coral;
		}

		.box .pic {
			width: 450px;
			height:450px;
			border: 1px solid lightgray;
			margin: 50px auto 0;
		}

		.box .pic img {
			width: 100%;
			height: 100%;
		}

	</style>
</head>
<body>
	<div class="box">
		<h2>明星相册</h2>
		<ul>
			<li>
				<a href="https://img.alicdn.com/simba/img/TB1T8F2TMHqK1RjSZFESuwGMXXa.jpg" title="《楚乔传》,《花千骨》,《陆贞传奇》...">赵丽颖</a>
			</li>
			<li>
				<a href="https://img.alicdn.com/simba/img/TB1mxi7IgHqK1RjSZFPSuwwapXa.jpg" title="《倚天屠龙记》,《咱们经婚吧》,《爱无悔》...">高圆圆</a>
			</li>
			<li>
				<a href="https://img.alicdn.com/simba/img/TB1Kp09WXzqK1RjSZFvSuwB7VXa.jpg" title="《那年花开月正圆》,《甑环传》,《玉观音》...">孙俪</a>
			</li>
			<li>
				<a href="https://img.alicdn.com/simba/img/TB1cH06gnZmx1VjSZFGSuux2XXa.jpg" title="《还珠格格》,《武媚娘传奇》,《我不是潘金莲》...">范冰冰</a>
			</li>
		</ul>
		<div class="pic">
			<img src="images/zwt.png" alt="" id="img">
		</div>
		<p id="info"></p>
	</div>

	<script>
	//1.获取页面元素
	var pic = document.getElementsByTagName('a');
	var img = document.getElementById('img');
	var p = document.getElementById('info');

	//2.循环遍历a给每个a添加事件
	for(var i = 0;i < pic.length;i += 1){
		pic[i].onclick = function(){
                        //锁定选项卡效果
			for(var i = 0;i < pic.length;i += 1){
				pic[i].classList.remove('active');
			}
			this.classList.add('active');

			//3.获取基本信息
			var picUrl = this.href;
			var picName = this.title;
			var picInfo = this.innerHTML;
		
			img.src = picUrl;
			p.innerHTML = '<span style="color:coral;">' + picInfo + picName + '</span>';
			return false;
		};
	}

	</script>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post