zoukankan      html  css  js  c++  java
  • 随便写了个 js 的类

    class Elem {
    
        /**
            可以通过  #id | .class | <tag>| name  四种形式来查找元素
        **/
        constructor(selecter) {
    
            this.selecter = selecter;
            this.elements = [];
            selecter = this.selecter.replace(/#(.+)/, '$1');
            if (this.selecter != selecter){
                  this.element = this.elements[0] = document.getElementById(selecter);
            }else{
                selecter = this.selecter.replace(/<(.+)>/, '$1');
                if (this.selecter != selecter) {
                    this.elements = document.getElementsByTagName(selecter);
    
                }else{
                    selecter = this.selecter.replace(/.(.+)/, '$1');
                    if (this.selecter != selecter){
                         this.elements = document.getElementsByClassName(selecter);
                    }else {
                        document.getElementsByName(this.selecter);
                    }
                }
            }
        }
    
        static one(selecter){
            this.element = this.elements = document.querySelector(selecter);
            return this;
        }
    
        static all(selecter) {
            this.elements = document.querySelectorAll(selecter);
            return this;
        }
    
        static save(name, value){
            sessionStorage.setItem(name,  value);
            return this;
        }
    
        static take(name){
            return  sessionStorage.getItem(name);
        }
    
        item(index=0){
            this.element = this.elements[index];
            return this;
        }
    
        get(index = 0) {
            return this.elements[index];
        }
    
        attr(name = 'value', value = undefined){
            return (value != undefined)? this.element.setAttribute(name, value) : this.element.getAttribute(name);
        }
    
        get attrs() {
            return this.element.attributes;
        }
    
        parent(){
            return this.element.parentNode;
        }
    
        childNode(index){
            return this.element.childNodes.item(index);
        }
    
        rmChild(index){
            this.element.removeChild(this.element.childNodes[index]);
            return this;
        }
    
        listen(event, func){
            this.element.addEventListener(event, func);
            return this;
        }
        
    
    }
        
    new Elem('#abc').listen('click',  ()=>{  alert('clicked me!');  })
    
    Elem.save('name', "张三").take('name')
  • 相关阅读:
    Shell脚本sed命令
    Shell脚本常用unix命令
    Shell的case语句
    3.5.2 数值之间的转换
    3.5.1 数学函数与常量
    3.5 运算符
    3.4.2 常量
    3.4.1 变量初始化
    3.4 变量
    Python异常捕捉的一个小问题
  • 原文地址:https://www.cnblogs.com/zbseoag/p/14238891.html
Copyright © 2011-2022 走看看