jQuery 简单实现
来源:互联网 发布:flask sql session 编辑:程序博客网 时间:2024/06/01 08:40
/** * JQuery 基础功能 * @param {[type]} ) {var $ [description] } * @return {[type]} [description] */var $ = (function () { var $ = function (selector, context) { return new $.prototype.init(selector, context); }; $.prototype.init = function (selector, ctx) { if(typeof selector === 'string'){ var context = ctx || document; var nodeList = context.querySelectorAll(selector); this.length = nodeList.length; for (var i = 0; i < this.length; i++) { this[i] = nodeList[i]; } }else if(typeof selector === 'object'){ this[0] = selector; this.length = 1; } return this; }; $.prototype.each = function (callback, args) { var length = this.length, i = 0; while (i < length) { callback.call(this[i], args); i += 1; } return this; }; $.prototype.css = function(arg1, arg2){ if(typeof arg1 === 'string' && !arg2){ return getEleStyle(this[0])[arg1]; } var sty_obj = arg1; if(typeof arg1 !== 'object' && arg2){ sty_obj = {}; sty_obj[arg1] = arg2; } this.each(function(){ for(var sty_name in sty_obj){ var sty = getEleStyle(this); sty[sty_name] = sty_obj[sty_name]; this.setAttribute('style', JSON.stringify(sty).replace(/[{}"]/g, '').replace(/[,]/g, ";") ); } }); return this; }; $.prototype.addClass = function(arg1){ this.each(function(){ this.setAttribute('class', (this.getAttribute('class')||'') + ' ' + arg1 ); }); return this; }; $.prototype.removeClass = function(arg1){ this.each(function(){ this.setAttribute('class', (this.getAttribute('class')||'').replace(arg1, '') ); }); return this; }; $.prototype.attr = function(arg1, arg2){ if(!arg2 && typeof arg1 === 'string'){ return this[0].getAttribute(arg1) || ''; } var attr_obj = arg2 ? {arg1:arg2} : arg1; this.each(function(){ for(var attr_name in attr_obj){ this.setAttribute(attr_name, attr_obj[attr_name]); } }); return this; }; $.prototype.html = function (arg1) { if (typeof arg1 == 'string') { this.each(function () { this.innerHTML = arg1; }); return this; } else { return this[0].innerHTML; } }; $.prototype.remove = function(){ this.each(function(){ this.parentNode.removeChild(this); }); }; $.prototype.append = function(arg1){ if(typeof arg1 === 'object'){ this.each(function(){ this.appendChild(arg1); }); }else if(typeof arg1 === 'string'){ this.each(function(){ this.innerHTML = arg1; }); } return this; }; $.prototype.bind = function(event, hander){ if (this[0].addEventListener) { this.each(function(){ this.addEventListener(event, hander); }); } else if (this[0].attachEvent) { this.each(function(){ this.attachEvent("on" + event, hander); }); } }; $.prototype.init.prototype = $.prototype; function getEleStyle (element){ var style = {}; var style_attr = element.getAttribute('style'); if(style_attr){ var style_arr = style_attr.split(';'); for(var item = 0; item < style_arr.length; item ++){ var item_val = style_arr[item].split(':'); style[item_val[0]] = item_val[1]; } } return style; } return $; })();
阅读全文
0 0
- JQuery实现简单菜单
- JQuery 实现简单过滤器
- jquery拖动简单实现
- 简单jQuery插件实现
- Jquery实现简单遮罩层
- jQuery 简单实现
- jquery简单轮播图实现
- 利用JQuery实现简单AJAX
- jquery实现简单文字提示
- 实现简单的jquery框架
- 简单瀑布流-jquery实现
- Jquery简单实现图书导航
- jQuery实现简单登陆判断
- jquery实现人物简单走动
- jquery幻灯片简单实现方法
- jquery 实现简单快乐农场
- jQuery实现简单选项卡
- jQuery实现简单弹出窗口
- 抽象类和接口有什么区别?(瞬联)
- 【QT】窗口位置
- RUSSELL 2000 QUANTITATIVE STOCK ANALYSIS IN R: SIX STOCKS WITH AMAZING, CONSISTENT GROWTH
- 一个单片机开发小菜鸟的职业规划的困惑
- Hibernate问题
- jQuery 简单实现
- CentOS7.2 设置静态ip
- 如何正确地写出单例模式
- 【蓝桥杯第六届javaB组国赛】表格计算
- Arcgis---the Vertical extent is too large for proper display
- 最全解析:大数据和机器学习有什么区别
- Linux中mysql无法启动 报错 the server quit without updating PID file的解决方法
- python实现支持向量机SVM算法
- Spring全家桶(八)AOP核心思想与AspectJ 5种类型通知