• js链式操作DOM节点的简单封装


    function Base() {
    		//把返回的节点对象保存到一个Base 对象的属性数组里
    		this.elements = [];
    		//获取id 节点
    		this.getId = function (id) {
    			this.elements.push(document.getElementById(id));
    			return this;
    		};
    		//获取name 节点数组
    		this.getName = function (name) {
    			var names = document.getElementsByName(name);
    			for (var i = 0; i < names.length; i ++) {
    			this.elements.push(targs[i]);
    			}
    			return this;
    		}
    		//获取元素节点数组
    		this.getTagName = function (tag) {
    			var tags = document.getElementsByTagName(tag);
    			for (var i = 0; i < tags.length; i ++) {
    			this.elements.push(tags);
    			}
    			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.css = function (attr, value) {
    	for (var i = 0; i < this.elements.length; i ++) {
    	this.elements[i].style[attr] = value;
    	}
    	return this;
    }
    Base.prototype.html = function (str) {
    	for (var i = 0; i < this.elements.length; i ++) {
    	this.elements[i].innerHTML = str;
    	}
    	return this;
    }
    
    var $ = function () {
        return new Base();
    };
    
    //调用 方法:$().getTagName("p").css("backgroundColor","red").html("title-----").click(function(){alert(1)})
    
  • 相关阅读:
    Samba
    百度贴吧
    baidu.com关键字查询
    vim 删除每行开头结尾空格
    read line(逐行读取)
    pycharm 激活
    rsync 启动脚本
    收藏
    elk
    sql is null
  • 原文地址:https://www.cnblogs.com/fangxuele/p/5428998.html
Copyright © 2020-2023  润新知