基于ES6的tinyJquery

原文地址:Bougie的博客

jQuery作为曾经Web前端的必备利器,随着MVVM框架的兴起,如今已稍显没落。但它操作DOM的便利性无出其右。我用ES6写了一个基于class简化版的jQuery,包含基础DOM操作,支持链式操作,仅供日常使用。当然,它不支持IE。

构造器(constructor)

构造一个tinyJquery对象。功能:基于基本选择器构造,包括id、class、tagName;基于原生DOM构造,将原生DOM对象转化为tinyJquery对象。为支持批量操作,tinyJquery构造器应包含复数的DOM。

class tinyJquery {
    constructor(name) {
        if (typeof name == 'string') {
            this.dom = document.querySelectorAll(name)
        } else if (name.constructor.name == 'NodeList' || Array.isArray(name)){
            this.dom = name
        } else {
            this.dom = [name]
        }
    }
}

使用$函数构建tinyJquery对象

function $(name) {
    return new tinyJquery(name)
}

方法(后续会渐渐完善)

event操作

// addEventListener
on(eventName, fn, bubble = false) {
    this.dom.forEach(i => {
        i.addEventListener(eventName, fn, !bubble)
    })
    return $(this.dom)
}
// removeEventListener
un(eventName, fn, bubble = false) {
    this.dom.forEach(i => {
        i.removeEventListener(eventName, fn, !bubble)
    })
    return $(this.dom)
}

class操作

// addClass
ac(className) {
    this.dom.forEach(i => {
        i.classList.add(className)
    })
    return $(this.dom)
}
// removeClass
rc(className) {
    this.dom.forEach(i => {
        i.classList.remove(className)
    })
    return $(this.dom)
}
// toggleClass
tc(className) {
    this.dom.forEach(i => {
        i.classList.toggle(className)
    })
    return $(this.dom)
}
// containClass
cc(className) {
    let flag = false
    this.dom.forEach(i => {
        if(i.classList.contains(className)) flag = true
    })
    return flag
}

属性操作

// set inline style
css(obj) {
    this.dom.forEach(v => {
        Object.keys(obj).forEach(i => {
            v.style[i] = obj[i]
        })
    })
    return $(this.dom)
}
// get or set input value
val(val) {
    if(val) {
        this.dom[0].value = val
        return $(this.dom)
    } else {
        return this.dom[0].value
    }
}

内容操作

// get or set dom innerHtml
html(val) {
    if(val) {
        this.dom.forEach(i => {
            i.innerHTML = val
        })
        return $(this.dom)
    } else {
        return this.dom[0].innerHTML
    }
}
// get or set attribute
attr(key, val) {
    if(key && !val) {
        return this.dom[0].getAttribute(key)
    } else {
        this.dom.forEach(i => {
            i.setAttribute(key, val)
        })
        return $(this.dom)
    }
}

表单操作

// get JSONData
serializeObject() {
    let dom = this.dom[0], obj = {}
    dom.querySelectorAll('input, textarea').forEach(i => {
        obj[i.getAttribute('name')] = i.value
    })
    return obj
}
// get FormData
serializeForm() {
    let dom = this.dom[0], form = new FormData()
    dom.querySelectorAll('input, textarea').forEach(i => {
        form.append(i.getAttribute('name'), i.value)
    })
    return form
}

2018-04-16更新

Dom获取

// parent
parent() {
    return $(this.dom[0].parentNode)
}
// siblings
siblings() {
    let dom = this.dom[0]
    var a = [];
    var p = dom.parentNode.children;
    for (var i = 0, pl = p.length; i < pl; i++) {
        if (p[i] !== dom) a.push(p[i]);
    }
    // console.log(Array.isArray(a))
    return $(a)
}

遍历

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

推荐阅读更多精彩内容

  • 第3章 基本概念 3.1 语法 3.2 关键字和保留字 3.3 变量 3.4 数据类型 5种简单数据类型:Unde...
    RickCole阅读 5,170评论 0 21
  • Scala与Java的关系 Scala与Java的关系是非常紧密的!! 因为Scala是基于Java虚拟机,也就是...
    灯火gg阅读 3,505评论 1 24
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,785评论 18 399
  •   JavaScript 与 HTML 之间的交互是通过事件实现的。   事件,就是文档或浏览器窗口中发生的一些特...
    霜天晓阅读 3,554评论 1 11
  • 无知 叛逆 疼痛 忙碌 成熟 陪伴 自我 天堂
    短发司机阅读 227评论 0 0