自己撸一个jQuery

呐,来研究一下jQuery的实现原理,自己撸一个简单版的来一试究竟吧!

选取元素

按照我们使用jQuery的使用步骤来一步步研究实现,所以,第一步当然就是从选取元素开始了。

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
}

不过我们在通过nodelist = document.querySelectorAll(nodes)获取元素的时候会发现一点小问题,那就是获取到的伪数组原型链可能会有点复杂,所以在这里我们可以做一个小小的优化。

window.jQuery = function(nodeOrSelector) {
    let nodes = {};
    if (typeof nodeOrSelector === 'string') {
        let temp = document.querySelectorAll(nodeOrSelector) //获取伪数组
        for (let i = 0; i < temp.length; i++) {
            nodes[i] = temp[i]
        }
        nodes.length = temp.length
    } else if (nodeOrSelector instanceof Node) {
        // 为了保证都是伪数组统一操作方式
        nodes = {
            0: nodeOrSelector,
            length: 1
        }
    }
    return nodes
}

自定义API

实现addclass

window.jQuery = function(nodeOrSelector) {
    let nodes = {};
    if (typeof nodeOrSelector === 'string') {
        let temp = document.querySelectorAll(nodeOrSelector) //获取伪数组
        for (let i = 0; i < temp.length; i++) {
            nodes[i] = temp[i]
        }
        nodes.length = temp.length
    } else if (nodeOrSelector instanceof Node) {
        // 为了保证都是伪数组统一操作方式
        nodes = {
            0: nodeOrSelector,
            length: 1
        }
    }

    nodes.addClass = function(classes) {
        if (typeof classes === "string") {
            classes = classes.split(',')  //
        }
        Array.prototype.forEach.call(classes, (value) => {
            for (let i = 0; i < nodes.length; i++) {
                nodes[i].classList.add(value)
            }
        })
    }
    return nodes
}

实现setText

window.jQuery = function(nodeOrSelector) {
    let nodes = {};
    if (typeof nodeOrSelector === 'string') {
        let temp = document.querySelectorAll(nodeOrSelector) //获取伪数组
        for (let i = 0; i < temp.length; i++) {
            nodes[i] = temp[i]
        }
        nodes.length = temp.length
    } else if (nodeOrSelector instanceof Node) {
        // 为了保证都是伪数组统一操作方式
        nodes = {
            0: nodeOrSelector,
            length: 1
        }
    }

    nodes.addClass = function(classes) {
        if (typeof classes === "string") {
            classes = classes.split(',')  //
        }
        Array.prototype.forEach.call(classes, (value) => {
            for (let i = 0; i < nodes.length; i++) {
                nodes[i].classList.add(value)
            }
        })
    }

    nodes.setText = function(text) {
        for (let i = 0; i < nodes.length; i++) {
            nodes[i].textContent = text
        }
    }
    return nodes
}

实现快捷使用自定义库

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

推荐阅读更多精彩内容

  • 通过jQuery,您可以选取(查询,query)HTML元素,并对它们执行“操作”(actions)。 jQuer...
    枇杷树8824阅读 679评论 0 3
  • 一、样式篇 第1章 初识jQuery (1)环境搭建 进入官方网站获取最新的版本 http://jquery.co...
    凛0_0阅读 3,526评论 0 44
  • 第一章 jQuery简介 1-1 jQuery简介 1.简介 2.优势 3.特性与工具方法 1-2 环境搭建 进入...
    mo默22阅读 1,652评论 0 11
  • 原文链接 http://blog.poetries.top/2016/10/20/review-jQuery 关注...
    前端进阶之旅阅读 16,718评论 18 503
  • 清净小庭院,闲来无别事。 何妨邀旧人,相呼出门去。 天高云淡远,秋水一何碧。 农人汗如雨,亦累亦还喜。
    余语于隅阅读 512评论 0 4