探索jQuery

·jQuery是什么?
jQuery 是一个高效、精简并且功能丰富的 JavaScript 工具库。它提供的 API 易于使用且兼容众多浏览器,这让诸如 HTML 文档遍历和操作、事件处理、动画和 Ajax 操作更加简单。(摘自jQuery中文文档)

其实jQuery就是接受一个元素,返回一个新的对象,这个对象有新的API

那接受一个元素,返回一个新的对象可以用如下代码表示

<ul id='ul'>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
</ul>

 <script>
        var jQuery = function (nodeOrSelector) {
            let nodes = {}
            if (typeof nodeOrSelector === 'string') {
                nodes = document.querySelectorAll(nodeOrSelector)
            }
            else if (nodeOrSelector instanceof Node) {
                nodes = { 0: nodeOrSelector, length: 1 }   
            }
            return nodes
            console.log(nodes)
        }
    </script>

image.png

为了方便使用,像Node一样加入window中,结果是一样的,注意返回的结果统一为伪数组。(重点理解)

<script>
        window.jQuery = function (nodeOrSelector) {
            let nodes = {}
            if (typeof nodeOrSelector === 'string') { 
                nodes = document.querySelectorAll(nodeOrSelector)//伪数组
            }
            else if (nodeOrSelector instanceof Node) {
                nodes = { 0: nodeOrSelector, length: 1 }   //创建伪数组
            }
            return nodes
            console.log(nodes)
        }
    </script>
image.png

想要让这个返回的对象有新的API,就利用对象的特点,在nodes对象中添加。

<script>
        window.jQuery = function (nodeOrSelector) {
            let nodes = {} //返回的对象!!!
            if (typeof nodeOrSelector === 'string') {
                nodes = document.querySelectorAll(nodeOrSelector)
            }
            else if (nodeOrSelector instanceof Node) {
                nodes = { 0: nodeOrSelector, length: 1 }   
            }

            //////////////////////添加addClass属性
           nodes.addClass = function (classes) {
                if (classes instanceof Array) {
                    classes.forEach(function (value) {
                        for (let i = 0; i < nodes.length; i++) {
                            nodes[i].classList.add(value)
                        }
                    })
                }
                else {
                    for (let i = 0; i < nodes.length; i++) {
                        nodes[i].classList.add(classes)
                    }
                }
            }
            ////////////////////////////////////////////

            return nodes
            
        }
    </script>

这样就可以通过addClass增加class

var $li = jQuery('li')
$li.addClass('red')

var $ul = jQuery(ul)
$ul.addClass(['blue','red'])

image.png

增加获取text或者修改text的API

window.jQuery = function (nodeOrSelector) {
            let nodes = {}
            if (typeof nodeOrSelector === 'string') {
                nodes = document.querySelectorAll(nodeOrSelector)
            }
            else if (nodeOrSelector instanceof Node) {
                nodes = { 0: nodeOrSelector, length: 1 }
            }

            nodes.addClass = function (classes) {
                if (classes instanceof Array) {
                    classes.forEach(function (value) {
                        for (let i = 0; i < nodes.length; i++) {
                            nodes[i].classList.add(value)
                        }
                    })
                }
                else {
                    for (let i = 0; i < nodes.length; i++) {
                        nodes[i].classList.add(classes)
                    }
                }
            }
            //////////////////////添加text属性
            nodes.text = function (text) {
                if (text === undefined) {
                    var texts = []
                    for (let i = 0; i < nodes.length; i++) {
                        texts.push(nodes[i].textContent)
                    }
                    return texts
                }
                else {
                    for (let i = 0; i < nodes.length; i++) {
                        nodes[i].textContent = text
                    }
                }
            }
              ////////////////////////////////////////////
                return nodes

            }

通过text获取或者修改文本内容

<ul id='ul'>
        <li>小红</li>
        <li>小明</li>
        <li>小花</li> //修改成小黄
    </ul>

var $getText = jQuery('li')
var getText = $getText.text()
console.log(getText)
            
var $setText = jQuery('li:nth-child(3)')
$setText.text('小黄')
image.png

image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第一章 jQuery简介 1-1 jQuery简介 1.简介 2.优势 3.特性与工具方法 1-2 环境搭建 进入...
    mo默22阅读 1,634评论 0 11
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,879评论 1 45
  • 一、样式篇 第1章 初识jQuery (1)环境搭建 进入官方网站获取最新的版本 http://jquery.co...
    凛0_0阅读 3,494评论 0 44
  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 9,395评论 0 3
  • 1.JQuery 基础 改变web开发人员创造搞交互性界面的方式。设计者无需花费时间纠缠JS复杂的高级特性。 1....
    LaBaby_阅读 1,406评论 0 2