Home >Web Front-end >JS Tutorial >Introduction to jquery principles and learning skills_jquery
Everyone must be familiar with JQuery. At present, during the implementation process of many web projects, taking into account the compatibility of each browser's native JS API, most of them choose JQuery or a framework similar to JQuery to develop web page effects. JQuery is easy to get started with and easy to learn. Even developers who are new to JQuery can quickly use it in projects with the help of the JQuery manual.
Although JQuery is relatively simple, it is not so easy to fully master it and use it quickly and flexibly. It provides many methods, covering various knowledge aspects of web development, so you must fully master these knowledge points. Personally, I think You still need to have an in-depth understanding of jQuery and classify and organize these knowledge points so that you will not be confused when facing some JQuery codes and know which method to use to achieve a certain special effect is the best practice. Quickly use JQuery for project development.
Simple simulation of JQuery
The code in JQuery is notoriously tricky, and there are too many tricks and tricks in it. If you want to learn JQuery through source code, it will be difficult to do it without a certain foundation of skills. So let’s write a very simple library to simulate JQuery for everyone to understand.
Overall code
(function(window) { var doc = window.document; // -------------代码段二 ------------------ var JQuery = function(selector) { return new JClass(selector); } // --------------- 代码段三 ---------------- JQuery.html = function(obj) { if(obj && obj.nodeType === 1) { return obj.innerHTML; } } // ---------代码段一 -------------- var JClass = function(selector) { if (selector.nodeType ) { // 如果传入的是DOM元素 this.length = 1; }else if(selector.charAt(0) === '#') { //如果传入的是'#..'形式 this.length = 1; this[0] = doc.getElementById(selector.slice(1)); }else if(typeof selector === 'string') { //传入的是字符串,假设全部为html标签 ,如'div' 'p'等 var nodes = doc.getElementsByTagName(selector); this.length = nodes.length; for(var i=0,len=nodes.length;i<len;i++) { this[i] = nodes[i]; } }else { //都不是的话,就不识别咯 this.length = 0; } }; // ------------ 代码段四 -------------------- JQuery.prototype.html = function() { if(this[0]) { return JQuery.html(this[0]); } } JClass.prototype = JQuery.prototype; // ------------ 代码段五 --------------- window.$ = window.JQuery = JQuery; }(window));
First, let’s look at code snippet 1. We create a javascript reference type. We can create an instance object based on the reference type and the input parameters. The input parameters simulate JQuery selection parameters, but are not as powerful as JQuery. , supports some types.
We know that we can construct an instance object through new JClass(...), which is no problem, but JClass is also a function and can be called directly, and direct calling is not what we want As a result, in order to prevent JClass from being called directly by developers, we cannot expose JClass to developers, so we can only construct JClass instance objects through code snippets.
In code segment 2, we can expose JQuery to developers, because whether it is through new JQuery() or JQuery(), what is returned is a JClass instance object, which is the result we want .
In the process of web development, in order to reuse code, we often provide some tool methods to facilitate development. Since JQuery is completely exposed to developers, we can completely use these tool methods as static properties of JQuery. For details, please refer to code segment 3. Of course, we can also add other tools and methods in this way, such as text, val, attr, css.....
These tools and methods are really good. JClass instance objects (encapsulating DOM) also have some public methods, and these methods can also be implemented with the help of tool methods. So how to create public methods for JClass instance objects? This can be achieved through code segment 4 (if you don’t understand this, please refer to Baidu JS’s prototype inheritance concept).
Finally, through code segment 5, JQuery can be aliased $ and exposed as a global variable. Then introduce the code into the HTML file for testing, please refer to the following code:
<html> <head> <title>jquery-t2</title> <script src="JQ.js"></script> </head> <body> <div style="margin:10px 10px;"> <span id='result'> 点击看效果 </span> </div> <div id='tst'>div文本值</div> </body> <script> alert($.html(document.getElementById('result '))); //点击看效果 alert($('span').html()); //点击看效果 alert($('#tst').html()); //div文本值 </script> </html>
JQuery的知识点分类
通过上面模拟的JQuery库,我们应该可以大概知道JQuery的主要功能:通过强大的选择器获得DOM元素,然后针将这些DOM元素的日常操作封装成对应的方法,如取值、赋值、修改、删除等,同时JQuery还为网页开发提供了一些工具类方法,如each、ajax、isArray、extend等。所以总的来说,JQuery知识点可分为三大类:
JQuery太流行了,网上的资料太多太多,以致于很多初学者根本不知道从哪学起,这里看一篇博客,那里翻一翻源码,收集了大量的资料,最后发现根本没时间学习,即使花了大量的时间学习,学习到的JQuery也成不了一个体系,到最后全部忘记了。开发的时候又只能去翻手册,一直翻手册能写出好的代码么?不可能吧。
下面我分享一下个人认为比较好的资料供初学者一步一步学习。
JQuery的一些概念
学习永远都不是记忆,记忆的东西迟早会被遗忘。真正要学习一门技术,最基本的原则就是要理解它。学习JQuery也是,在学习JQuery的知识点时,你首先要理解一些概念来辅助你理解这些知识点。 JQuery中的概念主要有这三个: JQuery()、JQuery对象和DOM对象。
先说DOM对象,这个很简单,它定义了访问HTML文档对象的一套属性、方法和事件,没有JQuery前,我们通常直接操作DOM,比较熟悉的API有getElementById 、GetElementByTagName等。
JQuery,它在JQuery中有个别名 $ 。通过上面章节的模拟代码(对应模拟代码中的JQuery)可知,其实就是一个函数,说得更细致点就是JQuery对象的工厂方法,它可以根据不同的入参来构造JQuery对象,如:
JQuery除了能创建JQuery对象的同时,JQuery自身也有很多静态方法,也可以称为工具方法,如 each、ajax、trim等。这些工具方法不仅在网页设计时常常会被用到,而且也会被当作工具方法用来实现JQuery对象的原型方法。
JQuery对象,非常重要的一个概念,类似于模拟代码中的JClass实例,通常是通过JQuery构造出的实例。在JQuery中,我们常常见到的JQuery对象是这样的: $('#id') 、$('div')等。关于JQuery对象我们要明白以下几点:
1、JQuery对象继承了JQuery原型(prototype)的所有属性和方法
2、JQuery对象不是数组,但是采用了类似数组的结构来存储元素,而且存储的元素是通过选择器获取得到的DOM对象。参照上面章节的模拟代码(JClass实例对象),JQuery对象有个length属性,表示当前对象里存储DOM对象的个数,而这些通过选择获得的DOM对象,是采用下标为0、1、2、3 ... 作为属性名来进行存储的。所以,根据以上特性,我们完全可以通过下面这种方式来访问元素:
var objs = $('div'); for(var i=0,len=objs.length;i<length;i++) { var o = objs[i]; //获取dom元素 ... ... }
综合来说,JQuery、JQuery对象、DOM对象三者之间的关系是: JQuery是个工厂方法,用来构造JQuery对象; JQuery对象是个类数组对象,里面存储了DOM对象;
理解了三者之间的关系,我们在理解下面知识点时,相对来说也就比较容易了:
关于过滤选择器如何理解? 如$('div:first') 。
如果理解了JQuery对象的内部结构,理解JQuery的过滤选择器就很容易了,如 :first 其实就是取JQuery对象属性为'0'的对象(封装成JQuery对象),同理 :last取的是属性为 length-1 的元素,:eq(index)取的是属性为index的元素。
在JQuery中,如何验证某个元素是否为空?
var $o = $('div .class'); if($ == null) { // 错误的做法 //什么都没找到 ...... } //正确的做法 if($o.html() == null) { //什么都没找到 ... ... } //正确的做法 if($o.length) { //什么都没找到 ... ... }
JQuery对象和DOM对象如何转换?
var dom = document.getElementById('id'); // DOM对象转换成JQuery对象 var $dom = $(dom); // 参照JQuery对象的构造方式 // 将JQuery对象转换成DOM对象 for(var i=0,len=$dom.length;i<length;i++) { var o = $dom[i]; //获取dom元素 ... ... }
总结
关于JQuery,上手虽然简单,但如果要高效快速的使用开发,还是需要好好理解下JQuery的。以上只是个人学习过程中的一些感悟和整理,建议各位在学习时也按照自己的思维习惯形成一套自己的知识体系,一来便于日后的开发,二来也便于查阅别人的代码。最后附注一些有意思的小问题,各位有兴趣就思考下吧。
问题一. 你觉得下面哪种方式代码好一点?为什么?
// 方式一 var $text = $("#text"); var $ts = $text.text(); //方式二 var $text = $("#text"); var $ts = $.text($text);
问题二. 下面代码中的this是什么呢?大概实现原理有什么?
全选复制放进笔记 $('#box').click(function(){ var obj = this; ... ... }
以上就是关于jquery原理以及学习技巧介绍,内容很充实,信息量很大,需要同学耐心学习,希望从中有所收获吧。