jQuery基础知识

204 阅读1分钟
  1. $()函数里能传递的参数
  • css选择器
  • 原生DOM
  • Function
  • null
  • 其他

返回值:传入null返回一个空对象,传入Function是等DOM加载完毕后立即执行的函数,其他均返回一个类数组

2.JQuery整体架构封装详解

//通过使用闭包,使里面的变量私有
(function(){
        //挂载到window下使外部能够访问
	window.jQuery = window.$ = Jquery
			
	function Jquery(id){
		return new Init(id);//返回一个对象
	};
			
	let Init = Jquery.prototype.init = function(id){
		let dom = document.getElementById(id.slice(1));
		this[0] = dom;
		this.length = 1;
		return this;
	};
			
	Jquery.prototype.text = function(){
		console.log('text');
                return this;//支持链式操作
	};
			
	Jquery.prototype.css = function(){
		console.log('css');
                return this;//支持链式操作
	}
			
	Init.prototype = Jquery.prototype;//通过改变Init的prototype指向Jquery的原型来继承Jquery的方法
			
})();
console.log($('#dom'))
console.log($('#dom').text().css());//链式操作

3.jquery文本属性

  • text()-取值赋值均一组
  • html()-取值取第一个,赋值赋一组
  • val()-取值取第一个,赋值赋一组
  • 这三个方法均能接收一个函数,第一个参数是索引值,第二个参数是调用对应方法后的返回值,也能接收一个字符串,val()是被select调用的时候也能接收一个数组。

4.jquery属性、特性

  • 固有属性(特性):id、class、title、href、src、alt、type、value等等
  • 新增属性:自定义属性
  • 方法:attr()和prop()-两个方法均能接收一个对象
  • 取:均取第一个;attr能取所有罗列出来的属性,prop能取所有的特性,不能取自定义属性;
  • 设置:均能设置一组;attr能设置已经罗列出来的属性和自定义属性(checked特殊);prop能够设置特有属性;
  • removeAttr()和removeProp()
  • removeAttr移除罗列出来的属性,removeProp不能移除特性,只是把特性的值设置为undefined,不能移除自定义属性;

5.jquery属性之class属性

  • addClass()、removeClass()、hasClass()、toggleClass()

6.DOM的筛选遍历

筛选

  • odd、even、first、last、eq
  • prev、prevAll、next、nextAll、siblings
  • filter、not、is、slice、map、has

查找

  • children、find、end、add、andBack
  • offsetParent、parent、parents、closest