【JavaScript 封装库】Prototype 原型版发布!
来源:互联网 发布:视觉检测软件 编辑:程序博客网 时间:2024/06/01 09:53
/*源码作者: 石不易(Louis Shi)联系方式: http://www.shibuyi.net===================================================================================================程序名称: JavaScript 封装库 Prototype 版迭代版本: 无功能总数: 14 个功能介绍: 1. 实现代码连缀2. id / name / tagName / class 节点获取3. class 选择器添加与移除4. css 规则添加与移除5. 设置与获取元素内部文本6. 设置与获取css7. 实现 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;};// 获取与设置innerHTMLBase.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;};// 获取与设置CSSBase.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') { // W3Ccss.push(window.getComputedStyle(this.elements[i], null)[cssKey]);} else if (typeof this.elements[i].currentStyle != 'undefined') { // IE 6/7/8css.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;};// 添加样式规则RuleBase.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') { // W3Csheet.insertRule(ruleName + ' {' + ruleText + '}', position);} else if (sheet.addRule != 'undefined') { // IE 6/7/8sheet.addRule(ruleName, ruleText, position);}}return this;};// 移除样式规则RuleBase.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') { // W3Csheet.deleteRule(position);} else if (typeof sheet.removeRule != 'undefined') { // IE 6/7/8sheet.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/
0 0
- 【JavaScript 封装库】Prototype 原型版发布!
- 封装--javascript的原型(prototype)
- 封装--javascript的原型(prototype)
- 【PHP 模板引擎】Prototype 原型版发布!
- 封装——JavaScript的原型(prototype)
- JavaScript 利用prototype原型实现封装继承多态
- javascript prototype 原型链
- 悟透JavaScript原型prototype
- Javascript prototype 原型链
- javascript prototype原型
- 理解JavaScript原型prototype
- javascript原型prototype
- Javascript---原型prototype
- JavaScript中的原型prototype
- JavaScript:prototype原型
- [Javascript] Prototype 原型
- javascript原型prototype
- javascript prototype原型继承
- [LeetCode35]Search Insert Position
- LeetCode 22. Merge k Sorted Lists
- STL: priority_queue学习
- 醉牛记牌器
- 你的覅U盾是否科技
- 【JavaScript 封装库】Prototype 原型版发布!
- [LeetCode36]Valid Sudoku
- 【JavaScript 封装库】BETA 1.0 测试版发布!
- 苍狼敏捷需求用例分析方法简介并讲义下载
- 【JavaScript 封装库】BETA 2.0 测试版发布!
- 如何将安卓系统的手机屏幕实时同步显示在电脑上(包括无线与USB有线方式含DLNA)
- 開物成務章
- 黑马程序员_javse字符串学习总结
- 聖人之道四焉章