gQuery : 简化 DOM 操作,兼容 jQuery

来源:互联网 发布:ubuntu gcc4.4 安装包 编辑:程序博客网 时间:2024/06/06 07:33

还是觉得 jQuery 的体积有点大,抽取其中常用的 DOM 操作函数,形成了 gQuery,去除空格后,只有不到 4k

 

(function (window, undefined) {    function eql(v, s) {        return (typeof v === "string" && v.toLowerCase() === s);    }    function obj(domElm) {        this.isSel = eql(domElm.nodeName, "select");        this.isChk = domElm.type === "radio" || domElm.type === "checkbox";        this.elm = domElm;        this.id = domElm.id;        this.attr = function (n, v) {            if (n != undefined && v != undefined) {                if (this.isChk && eql(n, "checked")) {                    this.elm.checked = v || eql(v, n);                } else if (n.toLowerCase() === "disabled") {                    this.elm.disabled = v || eql(v, n);                } else {                    this.elm.setAttribute(n, v);                }                return this;            }            else if (n) {                if (this.isChk && eql(n, "checked")) {                    return this.elm.checked;                } else if (eql(n, "disabled")) {                    return this.elm.disabled;                } else {                    return this.elm.getAttribute(n);                }            }        };        this.val = function (v) {            if (v != undefined) {                if (this.isChk) {                    this.elm.checked = v || eql(v, "checked");                } else if (this.isSel) {                    this.elm.selectedIndex = v;                } else {                    this.elm.value = v;                }                return this;            }            else {                var valu = this.elm.value;                if (this.isChk) {                    valu = this.elm.checked;                } else if (this.isSel) {                    valu = this.elm.selectedIndex;                }                return valu;            }        };        this.ready = function (e) {            if (window.addEventListener) {                window.addEventListener('load', e, false);            } else {                window.attachEvent('onload', e);            }        };        this.click = function (e) {            if (e) {                this.elm.onclick = e;            } else {                this.elm.click();            }        };        this.focus = function () {            if (!this.elm.disabled) {                this.elm.focus();            }        };        this.hide = function () {            this.elm.style.display = "none";        };        this.show = function () {            this.elm.style.display = "";        };        this.empty = function () {            while (this.elm.firstChild) {                this.elm.removeChild(this.elm.firstChild);            }            this.elm.innerHTML = "";        };        this.append = function (S) {            this.elm.innerHTML += S;        };        this.html = function (S) {            this.elm.innerHTML = S;        };    }    var br = {};    function bf(b, v) {        br.name = b; br[b] = true; br.version = v;    }    var ua = navigator.userAgent.toLowerCase();    var s;    (s = ua.match(/msie ([\d.]+)/)) ? bf("msie", s[1]) :         (s = ua.match(/firefox\/([\d.]+)/)) ? bf("firefox", s[1]) :         (s = ua.match(/chrome\/([\d.]+)/)) ? bf("chrome", s[1]) :         (s = ua.match(/opera.([\d.]+)/)) ? bf("opera", s[1]) :         (s = ua.match(/version\/([\d.]+).*safari/)) ? bf("safari =", s[1]) : 0;    var ls = [];    var gQ = function (sel, ctx) {        if (!sel) { // Handle $(""), $(null), or $(undefined)            return this;        }        var isElm = sel.nodeType; // Handle $(DOMElement)        var isS = !isElm && (typeof sel === "string"); //HANDLE: $("#id")        var eid = isS ? ((sel.substr(0, 1) == "#") ? sel.substr(1) : sel) : "";        for (var i = 0; i < ls.length; i++) {            var o = ls[i];            if ((isElm && sel == o.elm) || (isS && eid == o.id)) {                return o;            }        }        var domElm = isElm ? sel : (isS ? document.getElementById(eid) : undefined);        if (domElm) {            var o = new obj(domElm);            ls.push(o)            return o;        }    };    gQ.cache = ls;    gQ.browser = br;    gQ.ajax = function (opt) {        function _onStateChange(xhr, success, error) {            if (xhr.readyState == 4) {                var s = xhr.status;                if (s >= 200 && s < 300) {                    success(xhr.responseText);                } else {                    error(xhr, s);                }            } else {            }        }        function fn() { }        var url = opt.url || "";        var async = opt.async !== false;        var type = opt.type || 'GET';        var data = opt.data || null;        var success = opt.success || fn;        var error = opt.error || fn;        type = type.toUpperCase();        if (type == 'GET' && data) {            var args = "";            if (typeof data == 'string') {                args = data;            } else if (typeof data == 'object') {                var arr = new Array();                for (var k in data) {                    var v = data[k];                    arr.push(k + "=" + v);                }                args = arr.join("&");            }            url += (url.indexOf('?') == -1 ? '?' : '&') + args;            data = null;        }        var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');        xhr.onreadystatechange = function () {            _onStateChange(xhr, success, error);        };        xhr.open(type, url, async);        if (type == 'POST') {            xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;');        }        xhr.send(data);        return xhr;    };    window.$ = gQ;})(window);


 

原创粉丝点击