jQuery事件处理


一、Jquery事件处理

1、

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>jQuery事件处理</title>
		<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
	</head>
	<body>
		<button class="xxx">按钮一</button>
		<button class="xxx">按钮二</button>
		<button class="xxx">按钮三</button>
	</body>
	
	<script>
		$(".xxx").click( function(){
			var je = $(this);   //this为DOM对象, $(this)为jQuery对象
			alert("点中了:"+je.html());
			
		});
		
	</script>
</html>

2、

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>jQuery事件处理2</title>
		<script type="text/javascript" src="js/jquery-3.3.1.js"></script>

	</head>
	<body>
		<button class="xxx">按钮一</button>
		<button class="xxx">按钮二</button>
		<button class="xxx">按钮三</button>
	</body>
	
	<script>
		
		function initEvenSupport()
		{
			/**/
			$(".xxx").click(function(){
				var je = $(this);
				alert("选中了:"+je.html());
			});	
		}
		
		//ready也是一个事件,表示页面加载完成
		$(document).ready(function(){
			//等页面加载完成了,再添加事件回调
			initEvenSupport();
		});
	</script>
</html>

二、单项列表

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>单选列表</title>
		<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
		
		<style>
			/*菜单面板*/
			.menu{
				width: 300px;
				background-color: cornflowerblue;
			}
			
			/*菜单面板+单项*/
			.menu .item{
				padding: 8px;
				color: #fff;
				text-align: center;
				user-select: none; /*文字不可选中,(需考虑兼容性-webkit-moz*/
			}
			/*菜单面板+单项+hover*/
			.menu .item:hover{
				background-color: #eee4;
				border-color: #888;
				border-width: 1px 0px;
				color: #444;
			}
			/*菜单面板+单项+选中状态(子选择器.menu元素下的.selected元素*/
			.menu .select{
				background-color: #eee8;
				border-color: #888;
				border-width: 1px 0px;
				color: #444;
			}
		</style>
	</head>
	<body>
		<div class="menu">
			<div class="item">2019-09-01</div>
			<div class="item">2019-09-02</div>
			<div class="item">2019-09-03</div>
			<div class="item">2019-09-04</div>
			<div class="item">2019-09-05</div>
			<div class="item">2019-09-06</div>
			<div class="item">2019-09-07</div>
			<div class="item">2019-09-08</div>
			<div class="item">2019-09-09</div>
		</div>
	</body>
	<script>
		$(".item").click(function(){
			
			//把旧的选中项去掉选中状态
			//注:removeClass()参数里不需要加点号
			//用上了子选择器
			$(".menu .select").removeClass("select");
			
			//给点中的添加选中状态
			$(this).addClass("select");
			
			//取出点中项的信息
			var str = $(this).html();
			alert("选中了:"+str);
		});
	</script>
</html>

三、对话框

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>对话框</title>
		<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
		
		<style>
			
			/*全景窗口,指定left top right bottom*/
			.Js-dialog{
				display: none; /*默认隐藏*/
				position: fixed;
				z-index: 1000;  /*确保遮住后面所有的元素*/
				left: 0px;
				top: 0px;
				right: 0px;
				bottom: 0px;
				background-color: #ccc8;
				opacity: 1;   /*不透明度*/
			}
			
			/*前景,对话框窗口*/
			.Js-dialog .frame{
				position: relative;
				width: 350px;
				min-height: 150px;
				margin: 150px auto;  /*居中显示*/
				background-color: #fff;
				border-radius: 2px;
				border: 1px solid #ccc;
				box-shadow: 1px 1px 3px #aaa;
			}
			
			/*右上角的关闭对话框,绝对定位*/
			.Js-dialog .close{
				position: absolute;
				top: 0px;
				right: 0px;
				border: 1px solid #fff;
			}
			
			/*悬浮*/
			.Js-dialog .close:hover{
				border: 1px solid #ccc;
			}
			
			/*用户自定义内容区*/
			.Js-dialog .content{
				width: 100%;
				text-align: center;
				margin: 50px auto;
			}
			
			/*中间内容样式*/
			.poem{
				background-color: #CCCCCC;
				width: 300px;
				margin: 50px auto;
			}
			.poem p{
				text-align: center;
			}
			.ss{
				text-align: 	center;
			}
		</style>
	</head>
	<body>

		<div class="poem">
			<p> 春晓 </p> 
			<p> 作者:孟浩然	</p>
			<p>
				春眠不觉晓,处处闻啼鸟。<br>
				夜来风雨声,花落知多少。<br>
			</p>
		</div>
		
		<div  class="ss">
			<button onclick="test()">显示对话框</button>
		</div>
		
		<!--对话框定义-->
		<div id="mydlg" class="Js-dialog">
			<div class="frame">
				<img class="close" src="img/ic_close.png" />
				<div class="content">
					Js-dialog对话框
				</div>
			</div>
		</div>
	</body>
	<script>
		function test()
		{
			showDialog("#mydlg");
		}
		
		/*可以传送一个jQuery对象,也可以传一个字符串*/
		window.showDialog = function(selector)
		{
			var dlg = selector;
			if(selector.constructor == String)	 
				dlg = $(selector);
			
			// 点击关闭时,关闭对话框 
			$(".close", dlg).click(function(){
				dlg.hide(); // Closure 闭包
			});
						
			dlg.show();
		}
	</script>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值