jQuery 最简化实现

 window.jQuery = (selector) => {
            let nodes = {};
            if (typeof selector === 'string') {
                //是字符串
                let temp = document.querySelectorAll(selector);
                for (let i = 0; i < temp.length; i++) {
                    nodes[i] = temp[i]
                }
                nodes.length = temp.length;
            } else if (selector instanceof Node) {
                //直接是个Node节点
                nodes = {
                    0: selector,
                    length: 1
                }
            }
            nodes.addClass = (classList) => {
                classList.forEach(klass => {
                    for (let i = 0; i < nodes.length; i++) {
                        nodes[i].classList.add(klass)
                    }
                });
            }
            nodes.setText = (text) => {
                for (let i = 0; i < nodes.length; i++) {
                    nodes[i].textContent = text
                }
            }

            return nodes;
        }

        window.$ = jQuery

  

posted @ 2018-07-11 20:53  chaos_JS  阅读(408)  评论(0编辑  收藏  举报