/* * select方法是Sizzle选择器包的核心方法之一,其主要完成下列任务: * 1、调用tokenize方法完成对选择器的解析 * 2、对于没有初始集合(即seed没有赋值)且是单一块选择器(即选择器字符串中没有逗号), * 完成下列事项: * 1) 对于首选择器是ID类型且context是document的,则直接获取对象替代传入的context对象 * 2) 若选择器是单一选择器,且是id、class、tag类型的,则直接获取并返回匹配的DOM元素 * 3) 获取最后一个id、class、tag类型选择器的匹配DOM元素赋值给初始集合(即seed变量) * 3、通过调用compile方法获取“预编译”代码并执行,获取并返回匹配的DOM元素 * * @param selector 已去掉头尾空白的选择器字符串 * @param context 执行匹配的最初的上下文(即DOM元素集合)。若context没有赋值,则取document。 * @param results 已匹配出的部分最终结果。若results没有赋值,则赋予空数组。 * @param seed 初始集合 */ function select(selector, context, results, seed) { var i, tokens, token, type, find, // 调用tokenize函数解析selector match = tokenize(selector); // 若没有提供初始集合 if (!seed) { // Try to minimize operations if there is only one group // 若只有一组选择器,即选择器字符串没有逗号 if (match.length === 1) { // Take a shortcut and set the context if the root selector // is an ID /* * 下面代码是用来处理根选择器是ID类型的快捷方式 * * 在此使用slice[0]来创建一个新的集合, * 确保原有的集合不会被之后代码变更掉 */ tokens = match[0] = match[0].slice(0); /* * 若选择器是以id类型开始,且第二个是关系符(即+~>或空格), * 则获取id所属对象作为context继续完成后续的匹配 * * 此处的条件判断依次为: * tokens.length > 2 :若tokens有两个以上的选择器 * (token = tokens[0]).type === "ID" :第一个选择器的类型为ID(即以#开头的), * support.getById :支持getElementById函数 * context.nodeType === 9 :context对象是document * documentIsHTML :当前处理的是HTML代码 * Expr.relative[tokens[1].type] :第二个tokens元素是一个关系(即+~>或空格) * 在满足上面所有条件的情况下,执行if内的语句体 */ if (tokens.length > 2 && (token = tokens[0]).type === "ID" && support.getById && context.nodeType === 9 && documentIsHTML && Expr.relative[tokens[1].type]) { // 将当前上下文指向第一个ID选择器指定的节点对象 context = (Expr.find["ID"](token.matches[0].replace( runescape, funescape), context) || [])[0]; // 若当前上下文内没有指定ID对象,则直接返回results if (!context) { return results; } // 选择器字符串去掉第一个ID选择器 selector = selector.slice(tokens.shift().value.length); } // Fetch a seed set for right-to-left matching /* * 下面while循环的作用是用来根据最后一个id、class、tag类型的选择器获取初始集合 * 举个简单例子:若选择器是"div[title='2']", * 代码根据div获取出所有的context下的div节点,并把这个集合赋给seed变量, * 然后在调用compile函数,产生预编译代码, * 预编译代码完成在上述初始集合中执行[title='2']的匹配 * * 首先,检查选择器字符串中是否存在与needsContext正则表达式相匹配的字符 * 若没有,则将依据选择器从右到左过滤DOM节点 * 否则,将先生成预编译代码后执行(调用compile方法)。 */ /* * "needsContext" : new RegExp("^" + whitespace * + "*[>+~]|:(even|odd|eq|gt|lt|nth|first|last)(?:\\(" * + whitespace + "*((?:-\\d)?\\d*)" + whitespace * + "*\\)|)(?=[^-]|$)", "i") * needsContext用来匹配选择器字符串中是否包含下列内容: * 1、>+~三种关系符 * 2、:even、:odd、:eq、:gt、:lt、:nth、:first、:last八种伪类 * 其中,(?=[^-]|$)用来过滤掉类似于:first-child等带中杠的且以上述八个单词开头的其它选择器 */ i = matchExpr["needsContext"].test(selector) ? 0 : tokens.length; while (i--) { token = tokens[i]; // Abort if we hit a combinator // 遇到关系符跳出循环 if (Expr.relative[(type = token.type)]) { break; } if ((find = Expr.find[type])) { // Search, expanding context for leading sibling // combinators /* * rsibling = new RegExp(whitespace + "*[+~]") * rsibling用于判定token选择器是否是兄弟关系符 */ if ((seed = find(token.matches[0].replace( runescape, funescape), rsibling .test(tokens[0].type) && context.parentNode || context))) { // If seed is empty or no tokens remain, we can // return early // 剔除刚用过的选择器 tokens.splice(i, 1); selector = seed.length && toSelector(tokens); /* * 若selector为空,说明选择器仅为单一id、class、tag类型的, * 故直接返回获取的结果,否则,在获取seed的基础上继续匹配 */ if (!selector) { push.apply(results, seed); return results; } break; } } } } } // Compile and execute a filtering function // Provide `match` to avoid retokenization if we modified the // selector above /* * 先执行compile(selector, match),它会返回一个“预编译”函数, * 然后调用该函数获取最后匹配结果 */ compile(selector, match)(seed, context, !documentIsHTML, results, rsibling.test(selector)); return results; }

实现方法:1、用“$("img").delay(毫秒数).fadeOut()”语句,delay()设置延迟秒数;2、用“setTimeout(function(){ $("img").hide(); },毫秒值);”语句,通过定时器来延迟。

区别:1、axios是一个异步请求框架,用于封装底层的XMLHttpRequest,而jquery是一个JavaScript库,只是顺便封装了dom操作;2、axios是基于承诺对象的,可以用承诺对象中的方法,而jquery不基于承诺对象。

修改方法:1、用css()设置新样式,语法“$(元素).css("min-height","新值")”;2、用attr(),通过设置style属性来添加新样式,语法“$(元素).attr("style","min-height:新值")”。

增加元素的方法:1、用append(),语法“$("body").append(新元素)”,可向body内部的末尾处增加元素;2、用prepend(),语法“$("body").prepend(新元素)”,可向body内部的开始处增加元素。

在jquery中,apply()方法用于改变this指向,使用另一个对象替换当前对象,是应用某一对象的一个方法,语法为“apply(thisobj,[argarray])”;参数argarray表示的是以数组的形式进行传递。

删除方法:1、用empty(),语法“$("div").empty();”,可删除所有子节点和内容;2、用children()和remove(),语法“$("div").children().remove();”,只删除子元素,不删除内容。

去掉方法:1、用“$(selector).removeAttr("readonly")”语句删除readonly属性;2、用“$(selector).attr("readonly",false)”将readonly属性的值设置为false。

on()方法有4个参数:1、第一个参数不可省略,规定要从被选元素添加的一个或多个事件或命名空间;2、第二个参数可省略,规定元素的事件处理程序;3、第三个参数可省略,规定传递到函数的额外数据;4、第四个参数可省略,规定当事件发生时运行的函数。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.