click事件和jquery选项卡

一. click事件

实现效果是点击切换按钮,可以重复的切换背景色

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>click 事件</title>
	<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
	<script type="text/javascript">
		$(function(){

			$('#btn').click(function(){

				$('.box').toggleClass('sty');
			});

		})
	</script>
	<style type="text/css">
		.box{
			width:200px;
			height:200px;
			background-color:gold;
		}

		.sty{
			background-color:green;
		}


	</style>
</head>
<body>
	<input type="button" name="" value="切换" id="btn">
	<div class="box"></div>
</body>
</html>

  

二. jquery选项卡

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>	
	<style type="text/css">
		.btns{
			width:500px;
			height:50px;
		}

		.btns input{
			width:100px;
			height:50px;
			background-color:#ddd;
			color:#666;
			border:0px;
		}

		.btns input.cur{
			background-color:gold;
		}


		.contents div{
			width:500px;
			height:300px;
			background-color: gold;
			display:none;
			line-height:300px;
			text-align:center;
		}

		.contents div.active{
			display: block;
		}



	</style>
	<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
	<script type="text/javascript">
		$(function(){
                                //#btns input表示第一个input元素
			        $('#btns input').click(function() {
				// this是原生的对象
				$(this).addClass('cur').siblings().removeClass('cur');

				//$(this).index() 获取当前按钮所在层级范围的索引值
				$('#contents div').eq($(this).index()).addClass('active').siblings().removeClass('active');

			});
		})
		
	</script>
</head>
<body>
	<div class="btns" id="btns">
		<input type="button" value="tab01" class="cur">
		<input type="button" value="tab02">
		<input type="button" value="tab03">
	</div>

	<div class="contents" id="contents">
		<div class="active">tab文字内容一</div>
		<div>tab文字内容二</div>
		<div>tab文字内容三</div>
	</div>
</body>
</html>

  

猜你喜欢

转载自www.cnblogs.com/regit/p/8979092.html