基于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))
    })
}
    原文作者:AIBBSHINBAJI
    原文地址: https://segmentfault.com/a/1190000014272029
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞