基於ES6的tinyJquery

原文地址:Bougie的博客javascript

jQuery做爲曾經Web前端的必備利器,隨着MVVM框架的興起,現在已稍顯沒落。但它操做DOM的便利性無出其右。我用ES6寫了一個基於class簡化版的jQuery,包含基礎DOM操做,支持鏈式操做,僅供平常使用。固然,它不支持IE。

構造器(constructor)

構造一個tinyJquery對象。功能:基於基本選擇器構造,包括id、class、tagName;基於原生DOM構造,將原生DOM對象轉化爲tinyJquery對象。爲支持批量操做,tinyJquery構造器應包含複數的DOM。css

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對象html

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))
    })
}
相關文章
相關標籤/搜索