为了账号安全,请及时绑定邮箱和手机立即绑定

jQuery选择器和事件

标签:
JQuery

一、jQuery选择器   

<!DOCTYPE html><html>	<head>		<meta charset="UTF-8">		<title></title>		<script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="jquery-1.10.1.min.js"></script>		<script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="Selector.js"></script>	</head>	<body>		<p>p1</p>		<p class="pclass">p2</p>		<button>Click me</button>	</body></html>$(document).ready(function(){	$("button").click(function(){		$(".pclass").text("p元素被修改了");	})});


二、jQuery事件

    1.jQuery事件:

        常用事件方法

        绑定事件

        解除绑定事件

        事件的目标

        事件的冒泡

        自定义事件

<!DOCTYPE html><html>	<head>		<meta charset="UTF-8">		<title></title>		<script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="jquery-1.10.1.min.js"></script>		<script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="EventMethod.js"></script>	</head>	<body>		<button>Click me</button>	</body></html>$(document).ready(function(){//	$("button").click(function(){//	$("button").dblclick(function(){//	$("button").mouseenter(function(){	$("button").mouseleave(function(){		$(this).hide();	});});
<!DOCTYPE html><html>	<head>		<meta charset="UTF-8">		<title></title>		<script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="jquery-1.10.1.min.js"></script>		<script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="bindEvent.js"></script>	</head>	<body>		<button id="clickMeBtn">Click me</button>	</body></html>$(document).ready(function(){//	$("#clickMeBtn").click(function(){//		alert("hello");//	});//	$("#clickMeBtn").bind("click",clickHandler1);//	$("#clickMeBtn").bind("click",clickHandler2);//	$("#clickMeBtn").unbind("click",clickHandler1);		$("#clickMeBtn").on("click",clickHandler1);	$("#clickMeBtn").on("click",clickHandler2);	$("#clickMeBtn").off("click",clickHandler1);});function clickHandler1(e){	console.log("clickHandler1");}function clickHandler2(e){	console.log("clickHandler2");}
<!DOCTYPE html><html>	<head>		<meta charset="UTF-8">		<title></title>		<script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="jquery-1.10.1.min.js"></script>		<script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="EventTarget.js"></script>	</head>	<body>		<div style="width: 300px;height: 300px;background-color: #009B86;">			<li>A</li>			<li>B</li>			<li>C</li>			<li>D</li>		</div>	</body></html>$(document).ready(function(){	$("body").bind("click",bodyHandler);	$("div").bind("click",divHandler1);	$("div").bind("click",divHandler2);});function bodyHandler(event){	console.log(event);}function divHandler1(event){	console.log(event);//	event.stopPropagation();	event.stopImmediatePropagation();}function divHandler2(event){	console.log(event);}

  

<!DOCTYPE html><html>	<head>		<meta charset="UTF-8">		<title></title>		<script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="jquery-1.10.1.min.js"></script>		<script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="CustomEvent.js"></script>	</head>	<body>		<button id="ClickMeBtn">Click Me</button>	</body></html>var ClickMeBtn;$(document).ready(function(){	ClickMeBtn = $("#ClickMeBtn");	ClickMeBtn.click(function(){		var e = jQuery.Event("MyEvent");		ClickMeBtn.trigger(e);	});		ClickMeBtn.bind("MyEvent",function(event){		console.log(event);	});});

              







点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消