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')
  • 相关阅读:
    oracle中 start with .. connect by prior.. 用法简介
    Java中com.jcraft.jsch.ChannelSftp讲解
    linux修改系统时间和linux查看时区、修改时区的方法
    map.containsKey
    Struts2中struts.multipart.maxSize配置
    oracle定时器job的使用
    java的System.getProperty()方法可以获取的值
    夜间模式的开启与关闭,父模板的制作
    开始Flask项目
    完成登录与注册页面的前端
  • 原文地址:https://www.cnblogs.com/zbseoag/p/14238891.html
Copyright © 2011-2022 走看看