jQuery基础01

什么是jQuery

jQuery是一款JavaScript的库,作用是能让我们对HTML文档遍历和操作、事件处理、动画以及Ajax变得更简单。

jQuery的使用

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>HelloWorld</title>
		<!-- 引入jQuery -->
		<script src="../lib/jquery.js"></script>
		
		<script>
			//1.原生JS的固定写法
			window.onload = function(ev){ }
			//2.jQuery的固定写法
			$(document).ready(function(){
				alert("Hello jQuery");
			});
		</script>
	</head>
	<body>
	</body>
</html>

jQuery和js入口函数的区别

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>jQuery和js入口函数的区别</title>
		<script src="../lib/jquery.js">
			
		</script>
		<script>
			/* window.onload = function(ev){
				//1.通过原生的js入口函数可以拿到DOM元素
				var img = document.getElementsByTagName("img")[0];
				console.log(img);
				//2.通过原生的js入口函数可以拿到DOM元素的宽高
				var width = window.getComputedStyle(img).width;
				console.log("onload",width);
			} */
			
			$(document).ready(function(){
				//1.通过jQuery的入口函数可以拿到Dom函数
				var $img = $("img");
				console.log($img);
				//2.通过jQuery入口函数也可以拿到DOM元素的宽高(以前貌似不行)
				var $width = $img.width();
				console.log("ready",$width);
			});
			/* 
				1.原生的js如果编写了多个入口函数,后面编写的会覆盖前面编写的,只弹出hello 2
				2.jQuery中编写多个入口函数,后面的不会覆盖前面的,hello 3 与hello 4先后弹出
			 */
			/* window.onload = function(ev){
				alert("hello 1");
			}
			window.onload = function(ev){
				alert("hello 2");
			} */
			
			$(document).ready(function(){
				alert("hello 3");
			})
			$(document).ready(function(){
				alert("hello 4");
			})
		</script>
	</head>
	<body>
		<img src="https://imggif.gamersky.com/upimg/users/2020/07/22/origin_202007222258575594.gif" >
	</body>
</html>

jQuery入口函数的其他写法

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>jQuery入口函数的其他写法</title>
		<script src="../lib/jquery.js"></script>
		<script>
			//第一种写法
			$(document).ready(function(){
				alert("芜湖1~");
			});
			//第二种写法
			jQuery(document).ready(function(){
				alert("芜湖2~");
			});
			//第三种写法
			$(function(){
				alert("芜湖3~");
			});
			//第四种写法
			jQuery(function(){
				alert("芜湖4~");
			});
		</script>
	</head>
	<body>
	</body>
</html>

jQuery的冲突问题

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>jQuery冲突问题</title>
		<script src="../lib/jquery.js"></script>
		<script src="../lib/test.js"></script><!-- 会覆盖上面的jquery.js文件,引发冲突 -->
		<script>
			//冲突问题的两种解决方案
			//1.释放$的使用权
			//注意:释放操作必须在编写其他jQuery代码之前编写,释放之后就不能再使用$,改为使用jQuery
			//jQuery.noConflict();
			
			//2.自定义一个访问符号
			var xy = jQuery.noConflict();
			xy(function(){
				alert("hello c");
			});
		</script>
	</head>
	<body>
	</body>
</html>

核心函数

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>核心函数</title>
		<script src="../lib/jquery.js"></script>
		<script>
			//$();就代表调用jQuery的核心函数
			//1.接收一个函数
			$(function(){
				alert("hello!");
			
			//2.接收一个字符串
				//2.1接收一个字符串选择器
				//返回一个jQuery对象,对象中保存了找到的DOM元素
				var $box1 = $(".box1");
				var $box2 = $("#box2");
				console.log($box1);
				console.log($box2);
				//2.2接收一个字符串代码片段
				//返回一个jQuery对象,对象中保存了创建的DOM元素
				var $p = $("<p>我是段落</p>");
				console.log($p);
				$box1.append($p);
				//2.3接收一个DOM元素
				//会被包装成一个jQuery对象返回给我们
				var span = document.getElementsByTagName("span")[0];
				console.log(span);
				var $span = $(span);
				console.log($span);
			});
		</script>
	</head>
	<body>
		<div class="box1"></div>
		<div id="box2"></div>
		<span>我是span</span>
	</body>
</html>

对象

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>对象</title>
		<script src="../lib/jquery.js"></script>
		<script>
			$(function(){
				/* 
					1.jQuery对象是一个伪数组
					2.伪数组:有0到length-1的属性,并且有length属性
				 */
				var $div = $("div");
				console.log($div);
				
				var arr = [1,3,5];
				console.log(arr);
			});
		</script>
	</head>
	<body>
		<div>div1</div>
		<div>div2</div>
		<div>div3</div>
	</body>
</html>

静态方法和实例方法

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>静态方法和实例方法</title>
		<script src="../lib/jquery.js"></script>
		<script>
			//1.定义一个类
			function AClass(){
				
			}
			//2.给这个类添加一个静态方法
			//直接添加给类的就是静态方法
			AClass.staticMethod = function(){
				alert("staticMethod");
			}
			//静态方法通过类名调用
			AClass.staticMethod();
			
			//3.给这个类添加一个实例方法
			AClass.prototype.instanceMethod = function(){
				alert("instanceMethod");
			}
			//实例方法通过类的实例调用
			//创建一个实例对象(创建一个对象)
			var a = new AClass();
			//通过实例调用实例方法
			a.instanceMethod();
		</script>
	</head>
	<body>
	</body>
</html>

静态方法each方法

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>静态方法each方法</title>
		<script src="../lib/jquery.js"></script>
		<script type="text/javascript">
			var arr = [1,3,5,7,9];
			/* 
				第一个参数:遍历到的元素
				第二个参数:当前遍历到的索引
				注意点:
					1.原生的forEach方法只能遍历数组,不能遍历伪数组
			 */
			arr.forEach(function (value,index){
				console.log(value,index);
			})
			/*
			var obj = {0:1, 1:3, 2:5, 3:7, 4:9, length:5};
			obj.forEach(function(value,index){
				console.log(value,index);
			}); 
			报错:Uncaught TypeError: obj.forEach is not a function
			*/
		   
		   //利用jQuery的each静态方法遍历数组
		   /* 
			第一个参数:遍历到的元素
			第二个参数:当前遍历到的索引
			注意点:jQuery方法是可以遍历伪数组的
		   */
		   /* var obj = {0:1, 1:3, 2:5, 3:7, 4:9, length:5};
		   $.each(arr,function (index,value){
		   	console.log(index,value);
		   }); */
		   var obj = {0:1, 1:3, 2:5, 3:7, 4:9, length:5};
		   $.each(obj,function (index,value){
		   	console.log(index,value);
		   });
		</script>
	</head>
	<body>
	</body>
</html>

map方法

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>静态方法map方法</title>
		<script src="../lib/jquery.js"></script>
		<script>
			var arr = [1,3,5,7,9];
			var obj = {0:1, 1:3, 2:5, 3:7, 4:9, length:5};
			//利用原生js的map方法遍历
			/* 
				第一个参数:当前遍历到的元素
				第二个参数:当前遍历到的索引
				第三个参数:当前被遍历的数组
				注意:和原生的forEach一样,不能遍历伪数组
			 */
			/* 	
			arr.map(function(value,index,array){
			console.log(index,value,array);
			});
			*/
			/* 
			obj.map(function(value,index,array){
				console.log(index,value,array);
			}); 
			报错
			*/
		   /* 
				第一个参数:要遍历的数组
				第二个参数:每遍历一个元素之后执行的回调函数
				回调函数的参数:
					第一个参数:遍历到的元素
					第二个参数:遍历到的索引
		    $.map(arr,function(value,index){
				console.log(index,value);
			});
			
			注意:和jQuery中的each方法一样,map方法也可以遍历伪数组
		    */
			var res = $.map(obj,function(value,index){
				console.log(index,value);
				return value + index;//[1, 4, 7, 10, 13]
			});
			
			var res2 = $.each(obj,function (index,value){
				console.log(index,value);
				return value + index;//{0: 1, 1: 3, 2: 5, 3: 7, 4: 9, length: 5}
			});
			/* 
				jQuery中的each静态方法和map静态方法的区别:
					each静态方法默认的返回值就是,遍历谁就返回谁
					map静态方法默认的返回值是一个空数组
					
					each静态方法不持支在回调函数中对遍历的数组进行处理
					map静态方法可以在回调函数中通过return对遍历的数组进行处理,然后生成一个新的函数返回
			 */
			console.log(res);//[]
			console.log(res2);//{0: 1, 1: 3, 2: 5, 3: 7, 4: 9, length: 5}
		</script>
	</head>
	<body>
	</body>
</html>

其他方法

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>其他静态方法</title>
		<script src="../lib/jquery.js"></script>
		<script type="text/javascript">
			/*
				$.trim();
				作用:去除字符串两端的空格
				参数:需要去除空格的字符串
				返回值:去除空格之后的字符串
			 */
			var str = "   cqd   ";
			var res = $.trim(str);
			console.log("——————" + str + "——————");//——————   cqd   ——————
			console.log(res);//cqd
			
			/* 
				$.isWindow();
				作用:判断传入的对象是否为window对象
				返回值:true/false
			 */
			//真数组
			var arr = [1,3,5,7,9];
			//伪数组
			var arrlike = {0:1, 1:3, 2:5, 3:7, 4:9, length:5};
			//对象
			var obj = {"name":"cqd",age:"22"}; 
			//函数
			var fn = function(){};
			//window对象
			var w = window;
			//$.isWindow();
			
			var res2 = $.isWindow(w);
			console.log(res2);//true
			//传其他arr、arrlike。。。结果都是false
			
			/* 
				$.isArray();
				作用:判断传入的对象是否为真数组
				返回值:true/false
			 */
			var res3 = $.isArray(arr);//true
			console.log(res3);
			//传其他arrlike。。。结果都是false
			
			/* 
				$.isFunction();
				作用:判断传入的对象是否是一个函数
				返回值:true/false
				注意:jQuery框架本质上就是一个函数
				(function(window,undefined){
					
				})(window);
			 */
			var res4 = $.isFunction(fn);//true
			console.log(res4);
			
			var res5 = $.isFunction(jQuery);//true
			console.log(res5);
		</script>
	</head>
	<body>
	</body>
</html>	

holdReady方法(理解)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>holdReady方法(理解)</title>
		<script src="../lib/jquery.js"></script>
		<script>
			/* 
				$.holdReady(true);
				作用:暂停ready执行
			 */
			$.holdReady(true);
			$(document).ready(function(){
				alert("ready");
			});
		</script>
	</head>
	<body>
		<button>恢复ready事件</button>
		<script type="text/javascript">
			var btn = document.getElementsByTagName("button")[0];
			btn.onclick = function(){
				$.holdReady(false);
			}
			/*
				$.holdReady(false);
				作用:恢复ready执行
			 */
		</script>
	</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值