/*
源码作者: 石不易(Louis Shi)
联系方式: http://www.shibuyi.net
===================================================================================================
程序名称: JavaScript 封装库 Prototype 版
迭代版本: 无
功能总数: 14 个
功能介绍:
1. 实现代码连缀
2. id / name / tagName / class 节点获取
3. class 选择器添加与移除
4. css 规则添加与移除
5. 设置与获取元素内部文本
6. 设置与获取css
7. 实现 click 事件
*/ // 实例化入口
function $() {
return new Base();
} // 封装库构造方法
function Base() {
this.elements = [];
} // 获取id元素节点
Base.prototype.getId = function (id) {
this.elements.push(document.getElementById(id));
return this;
}; // 获取name元素节点
Base.prototype.getName = function (name, position) {
if (typeof position != 'undefined') { // 局部
var nodes = $().getTagName('*', position).elements;
for (var i = 0; i < nodes.length; i ++) {
if (nodes[i].getAttribute('name') == name) this.elements.push(nodes[i]);
}
} else { // 全局
var nodes = document.getElementsByName(name);
for (var i = 0; i < nodes.length; i ++) {
this.elements.push(nodes[i]);
}
}
return this;
}; // 获取tagName元素节点
Base.prototype.getTagName = function (tagName, position) {
if (typeof position != 'undefined') { // 局部
var nodes = $().getId(position).elements[0].getElementsByTagName(tagName);
} else { // 全局
var nodes = document.getElementsByTagName(tagName);
}
for (var i = 0; i < nodes.length; i ++) {
this.elements.push(nodes[i]);
}
return this;
}; // 获取class元素节点
Base.prototype.getClass = function (className, position) {
if (typeof position != 'undefined') { // 局部
var nodes = $().getTagName('*', position).elements;
} else { // 全局
var nodes = $().getTagName('*').elements;
}
for (var i = 0; i < nodes.length; i ++) {
if (nodes[i].className == className) this.elements.push(nodes[i]);
}
return this;
}; // 获取与设置innerHTML
Base.prototype.html = function (text) {
if (typeof text != 'undefined') { // 设置
for (var i = 0; i < this.elements.length; i ++) {
this.elements[i].innerHTML = text;
}
} else { // 获取
var html = [];
for (var i = 0; i < this.elements.length; i ++) {
html.push(this.elements[i].innerHTML);
}
return html;
}
return this;
}; // 获取与设置CSS
Base.prototype.css = function (cssKey, cssValue) {
if (typeof cssValue != 'undefined' || cssKey instanceof Array) { // 设置
for (var i = 0; i < this.elements.length; i ++) {
if (cssKey instanceof Array) {
var _cssKye, _cssValue, _css;
for (var j = 0; j < cssKey.length; j ++) {
if (cssKey[j].match(/([a-z]+)\s*=\s*([\w#]+)/i) != null) { // ['color=red', 'backgroundColor = green']
_css = cssKey[j].split(/\s*=\s*/);
_cssKey = _css[0];
_cssValue = _css[1];
this.elements[i].style[_cssKey] = _cssValue;
}
}
} else {
this.elements[i].style[cssKey] = cssValue;
}
}
} else { // 获取
var css = [];
for (var i = 0; i < this.elements.length; i ++) {
if (typeof window.getComputedStyle != 'undefined') { // W3C
css.push(window.getComputedStyle(this.elements[i], null)[cssKey]);
} else if (typeof this.elements[i].currentStyle != 'undefined') { // IE 6/7/8
css.push(this.elements[i].currentStyle[cssKey]);
}
}
return css;
}
return this;
}; // 检测class选择器
Base.prototype.hasClass = function (className) {
var results = [];
for (var i = 0; i < this.elements.length; i ++) {
results.push(!!this.elements[i].className.match(new RegExp('(\\s|^)' + className + '(\\s|$)')));
}
return results;
}; // 添加class选择器
Base.prototype.addClass = function (className) {
var space = '';
var results = this.hasClass(className);
for (var i = 0; i < results.length; i ++) {
if (!results[i]) {
if (this.elements[i].className != '') space = ' ';
this.elements[i].className += space + className;
}
}
return this;
}; // 移除class选择器
Base.prototype.removeClass = function (className) {
var results = this.hasClass(className);
for (var i = 0; i < results.length; i ++) {
if (results[i]) this.elements[i].className = this.elements[i].className.replace(new RegExp('(\\s|^)' + className + '(\\s|$)'), '');
}
return this;
}; // 添加样式规则Rule
Base.prototype.addRule = function (ruleName, ruleText, index, position) {
if (typeof index == 'undefined') index = 0;
if (typeof position == 'undefined') position = 0;
var sheet = document.styleSheets[index];
if (typeof sheet != 'undefined') {
if (typeof sheet.insertRule != 'undefined') { // W3C
sheet.insertRule(ruleName + ' {' + ruleText + '}', position);
} else if (sheet.addRule != 'undefined') { // IE 6/7/8
sheet.addRule(ruleName, ruleText, position);
}
}
return this;
}; // 移除样式规则Rule
Base.prototype.removeRule = function (index, position) {
if (typeof index == 'undefined') index = 0;
if (typeof position == 'undefined') position = 0;
var sheet = document.styleSheets[index];
if (typeof sheet != 'undefined') {
if (typeof sheet.deleteRule != 'undefined') { // W3C
sheet.deleteRule(position);
} else if (typeof sheet.removeRule != 'undefined') { // IE 6/7/8
sheet.removeRule(position);
}
}
return this;
}; // 鼠标 click 事件
Base.prototype.click = function (method) {
if (method instanceof Function) {
for (var i = 0; i < this.elements.length; i ++) {
this.elements[i].onclick = method;
}
}
return this;
};

关于 Prototype 原型版核心源码与实例演示的获取请移动至官网下载!

 

感谢大家积极评测给予意见!

官网地址:http://www.shibuyi.net

CNBlogs 博客:http://www.cnblogs.com/shibuyi/

CSDN 博客:http://blog.csdn.net/louis_shi/

ITeye 博客:http://shibuyi.iteye.com/

【JavaScript 封装库】Prototype 原型版发布!的更多相关文章

  1. 【PHP 模板引擎】Prototype 原型版发布!

    在文章的开头,首先要向一直关注我的人说声抱歉!因为原本是打算在前端框架5.0发布之后,就立马完成 PHP 模板引擎的初版.但我没能做到,而且一直拖到了15年元旦才完成,有很严重的拖延症我很惭愧,再次抱 ...

  2. 【JavaScript 封装库】BETA 5.0 测试版发布!

    JavaScript 前端框架(封装库) BETA 5.0 已于10月10日正式发布,今天开始提供 BETA 5.0 的 API 参考文献.相较于之前 5 个版本的发布都是草草的提供源代码,并没有很多 ...

  3. 【JavaScript 封装库】BETA 3.0 测试版发布!

    /* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =============================================== ...

  4. 【JavaScript 封装库】BETA 2.0 测试版发布!

    /* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =============================================== ...

  5. 【JavaScript 封装库】BETA 1.0 测试版发布!

    /* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =============================================== ...

  6. 【JavaScript 封装库】BETA 4.0 测试版发布!

    /* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =============================================== ...

  7. 【PHP 基础类库】Prototype 原型版教学文章!

    前言 大家好我是:石不易,今天我为大家带来了PHP基础类库原型版的教学文章,至此本人的作品线已分为三大类,分别是:JavaScript前端框架(封装库).PHP模板引擎.以及PHP基础类库.该类库历时 ...

  8. javascript中的prototype(原型)认识

    prototype实现了对象与对象的继承,在JS中变量,函数,几乎一切都是对象,而对象又有_ptoro_属性,这个属性就是通常说的原型,是用来指向这个对象的prototype对象,prototype对 ...

  9. 【JavaScript】关于prototype原型的一些链接

    http://www.cnblogs.com/slowsoul/archive/2013/05/30/3107198.html http://www.thinksaas.cn/group/topic/ ...

随机推荐

  1. java thread start到run:C++源码分析

    转:https://hunterzhao.io/post/2018/06/11/hotspot-explore-inside-java-thread-run/ 整体流程 java new Thread ...

  2. idea(3)-jetty配置

    1.jetty&jdk版本 9.3----->1.8 9.2----->1.7 8------->1.6 2.pom.xml <plugin> <group ...

  3. 问题:modbus_tk开发中遇到[Errno 98] Address already in use (已解决)

    案例: from modbus_tk import modbus_tcp,defines import time s = modbus_tcp.TcpServer(port=5300) def mai ...

  4. C. Permute Digits dfs大模拟

    http://codeforces.com/contest/915/problem/C 这题麻烦在前导0可以直接删除,比如 1001 100 应该输出11就好 我的做法是用dfs,每一位每一位的比较. ...

  5. H903

    Metadata-Version: 2.0Name: hackingVersion: 0.10.2Summary: OpenStack Hacking Guideline EnforcementHom ...

  6. (转)在 VMware 中安装 HMC

    在 VMware 中安装 HMC 原文:http://blog.csdn.net/ccie38499/article/details/14123493 http://www.54it.top/arch ...

  7. 05-spring整合jdbc-jdbc模板对象JdbcTemplate

    1 演示JdbcTemplate模板对象 1 导包 2 准备数据库 3 书写UserDao package www.test.dao; import java.util.List; import ww ...

  8. pat1066. Root of AVL Tree (25)

    1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...

  9. HDU 4578——Transformation——————【线段树区间操作、确定操作顺序】

    Transformation Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 65535/65536 K (Java/Others)T ...

  10. Spring Boot2中Spring Security导致Eureka注册失败

    将Spring Boot升级到2.0,Spring Cloud升级到Finchley.M8时,Eureka注册就报错了 Eureka Server配置: server.port=9011 spring ...