zoukankan      html  css  js  c++  java
  • day01

    /**
     * Created by zhouyan on 15/4/26.
     */
    
    
    //前台调用
    var $=function(){
        return new Base();
    }
    
    
    //基础库
    function Base(){
    
        //创建一个数组,来保存获取的节点和节点数组
        this.elements=[];
    
        //获取ID节点
        this.getId=function(id){
            this.elements.push(document.getElementById(id));
            return this;
        };
    
        //获取元素节点
        this.getTagName=function(tagName){
            var tags=document.getElementsByTagName(tagName);
            for(var i=0;i<tags.length;i++){
                this.elements.push(tags[i]);
            }
            return this;
        };
    
        //获取Class节点
        this.getClass=function(className,idName){
            var node = null;
            if(arguments.length == 2){
                node = document.getElementById(idName);
            }else{
                node = document;
            }
            var all=node.getElementsByTagName('*');
            for(var i=0;i<all.length;i++) {
                if (all[i].className == className) {
                    this.elements.push(all[i])
                }
            }
            return this;
        }
    
    }
    
    
    //设置css
    Base.prototype.css=function(attr,value){
        for(var i=0;i<this.elements.length;i++) {
            if(arguments.length == 1){
                if(typeof window.getComputedStyle !="undefined"){//W3C
                    return window.getComputedStyle(this.elements[i],null)[attr];
                }else if(typeof this.elements[i].currentStyle != 'undefined'){//IE
                    return this.elements[i].currentStyle[attr];
                }
    
                return this.elements[i].style[attr];
            }
            this.elements[i].style[attr] = value;
        }
        return this;
    }
    
    //设置innerHTML
    Base.prototype.html=function(str){
        for(var i=0;i<this.elements.length;i++) {
            if(arguments.length == 0){
                return this.elements[i].innerHTML;
            }
            this.elements[i].innerHTML = str;
        }
        return this;
    }
    
    //触发点击事件
    Base.prototype.click=function(fn){
        for(var i=0;i<this.elements.length;i++) {
            this.elements[i].onclick=fn;
        }
        return this;
    }
    
    //获取某一个节点
    Base.prototype.getElement =function(num){
        var element =this.elements[num];
        this.elements = [];
        this.elements[0] =element;
        return this;
    }
    
    //添加Class
    Base.prototype.addClass = function(className){
        for(var i=0;i<this.elements.length;i++){
            if(!this.elements[i].className.match(new RegExp('(\s|^)'+className+'(\s|$)'))) {
                this.elements[i].className += ' '+className;
            }
        }
        return this;
    }
    
    //移除Class
    Base.prototype.removeClass = function(className){
        for(var i=0;i<this.elements.length;i++){
            if(this.elements[i].className.match(new RegExp('(\s|^)'+className+'(\s|$)'))) {
                this.elements[i].className=this.elements[i].className.replace(new RegExp('(\s|^)'+className+'(\s|$)'),'');
            }
        }
        return this;
    }
    
    //添加link或style的css规则
    Base.prototype.addRule = function(num,selectorText,cssText,pos){
        var sheet = document.styleSheets[num];
        if(typeof sheet.insertRule != 'undefined'){   //w3c
            sheet.insertRule(selectorText+'{'+cssText+'}',pos);
        }else if(typeof sheet.addRule != 'undefined'){ //Ie
            sheet.addRule(selectorText,cssText,pos);
        }
        return this;
    }
    
    //移除link或style的css规则
    Base.prototype.removeRule = function(num,pos){
        var sheet = document.styleSheets[num];
        if(typeof sheet.deleteRule != 'undefined'){   //w3c
            sheet.deleteRule(pos);
        }else if(typeof sheet.removeRule != 'undefined'){ //Ie
            sheet.removeRule(pos);
        }
        return this;
    }
    

      

  • 相关阅读:
    让 vscode 作为 SpringBoot,Java,Maven,甚至于 JavaScript,C,C++,Python,Php等多语言的开发工具吧!
    MySQL 连接错误集锦
    前端开发工具库:包含事件委托,动画处理,以及大部分常用的前端工具
    初探 Node.js 框架:eggjs (环境搭配篇)
    如何快速搭建一个 Node.JS 项目并进入开发?
    关于 JavaSrcipt 前端开发的建议:模块化开发
    Spring-Session 会话共享 -> 基于 Redis 集群,内附各大错误合集,包括配置,类寻找不到、连接错误等
    Java Email 邮件发送
    CSS 显示或隐藏子元素
    Ubuntu美化及配置,常见问题解决方案(仿 Mac 风格)
  • 原文地址:https://www.cnblogs.com/serene92/p/4458262.html
Copyright © 2011-2022 走看看