jQuer-ish Library

<script type="text/javascript" src="http://shitake-crude-production.wdfiles.com/local--code/jquerish-library/1"></script>


/* * * * * * /
Copyright (c) 2019 7happy7
Released under the MIT license
https://opensource.org/licenses/mit-license.php
/ * * * * * */
 
const $ = (val) => {
    var $_sel_all_def = document.querySelectorAll(val)
    if($_sel_all_def.length > 1) {
        return $_sel_all_def;
    }
    else if($_sel_all_def.length == 1) {
        return $_sel_all_def[0];
    }
    return undefined;
}
const $new = (val, where_atr, pro, where) => {
    var $cEbox = document.createElement(val);
    if(where_atr == undefined) {
        return $cEbox;
    }
    else if(pro !== undefined && where == undefined) {
        if(where_atr.length && where_atr.length == pro.length) {
            for(var i = 0; i < pro.length; i++) {
                $cEbox.setAttribute(where_atr[i], pro[i]);
            }
            return $cEbox;
        }
        $cEbox.setAttribute(where_atr, pro);
        return $cEbox;
    }
    else if(where_atr !== undefined && pro == undefined) {
        if(where_atr.length) {
            for(var i = 0; i < where_atr.length; i++) {
                $cEbox = "";
                $cEbox = document.createElement(val);
                where_atr[i].appendChild($cEbox);
            }
            return undefined;
        }else {
            where_atr.appendChild($cEbox);
            return undefined;
        }
    }
    else if(where !== undefined) {
        if(where.length) {
            for(var i = 0; i < where.length; i++) {
                $cEbox = "";
                $cEbox = document.createElement(val);
                if(where_atr.length && where_atr.length == pro.length) {
                    for(var j = 0; j < pro.length; j++) {
                        $cEbox.setAttribute(where_atr[j], pro[j]);
                    }
                }else {
                    $cEbox.setAttribute(where_atr, pro);
                }
                where[i].appendChild($cEbox);
            }
            return undefined;
        }else {
            $cEbox.setAttribute(where_atr, pro);
            where.appendChild($cEbox);
            return undefined;
        }
    }
}
const attr = (elem, prop, attr) => {
    var x, y;
    if(prop == undefined) {
        if(elem.length) {
            for(var i = 0; i < elem.length; i++) {
                elem[i].removeAttribute("style");
            }
        }else {
            elem.removeAttribute("style");
        }
    }
    else if(!prop.match(/:|;/g)) {
        if(elem.length) {
            for(var i = 0; i < elem.length; i++) {
                y = attr;
                if(y == undefined) {
                    elem[i].removeAttribute(prop);
                }else {
                    elem[i].setAttribute(prop, y);
                }
            }
        }else {
            y = attr;
            if(y == undefined) {
                elem.removeAttribute(prop);
            }else {
                elem.setAttribute(prop, y);
            }
        }
    }else {
        var normalToCamel = (txt) => {
            return txt.replace(/\-./g, (txt) => {
                    return txt.charAt(1).toUpperCase();
            });
        };
        var propertyBase = prop.replace(/  /g, "").replace(/(\:|\;) /g, "$1").split(/(.*?):(.*?);|(.*?):(.*?)$/g).filter(v => v);
        var property = [];
        for(var i = 0; i < propertyBase.length/2; i++) {
            property.push([normalToCamel(propertyBase[i*2]), propertyBase[i*2+1]])
        }
        if(elem.length) {
            for(var i = 0; i < elem.length; i++) {
                for(var j = 0; j < property.length; j++) {
                    x = property[j][1];
                    if(x == "void") {x = ""}
                    elem[i].style[property[j][0]] = x;
                    if(elem[i].style.length == 0) {
                        elem[i].removeAttribute("style");
                    }
                }
            }
        }else {
            for(var i = 0; i < property.length; i++) {
                x = property[i][1];
                if(x == "void") {x = ""}
                elem.style[property[i][0]] = x;
                if(elem.style.length == 0) {
                    elem.removeAttribute("style");
                }
            }
        }
    }
}
 
// help
var $help_lib_text = `
$("#id");
attr($("#id"), "color: red;");
attr($("#id"), "color: void;");// delete color in style
attr($("#id"));// delete style
attr($("#id"), "class", "abc def");// add class
attr($("#id"), "class");// delete class
$new("span");// createElement
 
// cE and setAttribute
$new("span", "class", "sample");
$new("span", ["class", "data-x"], ["sample", ""]);
 
// cE and appendChild
$new("span", $("#id"));
 
// cE, sA and aC
$new("span", "class", "sample", $("#id"));
$new("span", ["class", "data-x"], ["sample", ""], $("#id"));
`;

http://shitake-crude-production.wdfiles.com/local--code/jquerish-library/2

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License