【转】原生js仿jquery一些常用方法
现在利用扩展原型的方法实现一些jquery函数:
1.显示/隐藏
|
1
2
3
4
5
6
7
8
9
10
|
//hide() Object.prototype.hide = function(){ this.style.display="none"; return this; } //show() Object.prototype.show = function(){ this.style.display="block"; return this; } |
return this的好处在于链式调用。
2.滑动 省略speed和callback的传入,因为要加一串判断和处理回调,代码量大
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
//slideDown() Object.prototype.slideDown = function(){ this.style.display = 'block'; if(this.clientHeight<this.scrollHeight){ this.style.height=10+this.clientHeight+"px"; var _this = this; setTimeout(function(){_this.slideDown()},10) }else{ this.style.height=this.scrollHeight+"px"; } } //slideUp() Object.prototype.slideUp = function(){ if(this.clientHeight>0){ this.style.height=this.clientHeight-10+"px"; var _this = this; setTimeout(function(){_this.slideUp()},10) }else{ this.style.height=0; this.style.display = 'none'; } } |
3.捕获/设置
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
//attr() Object.prototype.attr = function(){ if(arguments.length==1){ return eval("this."+arguments[0]); }else if(arguments.length==2){ eval("this."+arguments[0]+"="+arguments[1]); return this; } } //val() Object.prototype.val = function(){ if(arguments.length==0){ return this.value; }else if(arguments.length==1){ this.value = arguments[0]; return this; } } //html() Object.prototype.html = function(){ if(arguments.length==0){ return this.innerHTML; }else if(arguments.length==1){ this.innerHTML = arguments[0]; return this; } } //text()需要在html()结果基础上排除标签,会很长,省略 |
4.CSS方法
|
1
2
3
4
5
6
7
8
9
|
//css() Object.prototype.css = function(){ if(arguments.length==1){ return eval("this.style."+arguments[0]); }else if(arguments.length==2){ eval("this.style."+arguments[0]+"='"+arguments[1]+"'"); return this; } } |
5.添加元素
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
//append() Object.prototype.append = function(newElem){ this.innerHTML += newElem; return this; } //prepend() Object.prototype.prepend = function(newElem){ this.innerHTML = arguments[0] + this.innerHTML; return this; } //after() Object.prototype.after = function(newElem){ this.outerHTML += arguments[0]; return this; } //before() Object.prototype.before = function(newElem){ this.outerHTML = arguments[0] + this.outerHTML; return this; } |
6.删除/替换元素
|
1
2
3
4
5
6
7
8
9
10
11
|
//empty() Object.prototype.empty = function(){ this.innerHTML = ""; return this; } //replaceWith() Object.prototype.replaceWith = function(newElem){ this.outerHTML = arguments[0]; return this; } //remove() js自带,省略。 |
7.设置css类
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
//hasClass() Object.prototype.hasClass = function(cName){ return !!this.className.match( new RegExp( "(\\s|^)" + cName + "(\\s|$)") ); } //addClass() Object.prototype.addClass = function(cName){ if( !this.hasClass( cName ) ){ this.className += " " + cName; } return this; } //removeClass() Object.prototype.removeClass = function(cName){ if( this.hasClass( cName ) ){ this.className = this.className.replace( new RegExp( "(\\s|^)" + cName + "(\\s|$)" )," " ); } return this; } |
上面的设置CSS类也可以利用html5新API classList及contains实现 但不兼容IE8以下及部分火狐浏览器
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
Object.prototype.hasClass = function(cName){ return this.classList.contains(cName) } Object.prototype.addClass = function(cName){ if( !this.hasClass( cName ) ){ this.classList.add(cName); } return this; } Object.prototype.removeClass = function(cName){ if( this.hasClass( cName ) ){ this.classList.remove(cName); } return this; } |
9.选择器
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
//id或class选择器$("elem") function $(strExpr){ var idExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/; var classExpr = /^(?:\s*(<[\w\W]+>)[^>]*|.([\w-]*))$/; if(idExpr.test(strExpr)){ var idMatch = idExpr.exec(strExpr); return document.getElementById(idMatch[2]); }else if(classExpr.test(strExpr)){ var classMatch = classExpr.exec(strExpr); var allElement = document.getElementsByTagName("*"); var ClassMatch = []; for(var i=0,l=allElement.length; i<l; i++){ if(allElement[i].className.match( new RegExp( "(\\s|^)" + classMatch[2] + "(\\s|$)") )){ ClassMatch.push(allElement[i]); } } return ClassMatch; } } |
需要强调的是,选择器返回的结果或结果集包含的是htmlDOM,并非jquery的对象。大多数人都知道,document.getElementById("id")等价于jquery$("#id")[0],另外上面class选择器选择的结果如需使用,需要利用forEach遍历:
|
1
2
3
|
$(".cls").forEach(function(e){ e.css("background","#f6f6f6") }) |
10.遍历 siblings()和children()获取的结果也需要结合forEach使用
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
//siblings() Object.prototype.siblings = function(){ var chid=this.parentNode.children; var eleMatch = []; for(var i=0,l=chid.length;i<l;i++){ if(chid[i]!=this){ eleMatch.push(chid[i]); } } return eleMatch; } //children() 原生js已含有该方法,故命名为userChildren。 Object.prototype.userChildren = function(){ var chid=this.childNodes; var eleMatch = []; for(var i=0,l=chid.length;i<l;i++){ eleMatch.push(chid[i]); } return eleMatch; } //parent() Object.prototype.parent = function(){ return this.parentNode; } //next() Object.prototype.next = function(){ return this.nextElementSibling; } //prev() Object.prototype.prev = function(){ return this.previousElementSibling; } |
【转】原生js仿jquery一些常用方法的更多相关文章
- 原生js仿jquery一些常用方法
原生js仿jquery一些常用方法 下面小编就为大家带来一篇原生js仿jquery一些常用方法(必看篇).小编觉得挺不错的,现在就分享给大家,也给大家做个参考.一起跟随小编过来看看吧 最近迷上了原 ...
- [js插件开发教程]原生js仿jquery架构扩展开发选项卡插件
jquery插件一般是这么干的: $.fn.插件名称 = function(){}, 把插件的名称加在.fn上,在源码里面实际上是扩展到构造函数的原型对象上,如果你没看过jquery的源代码,或者你曾 ...
- 用原生js模仿jquery
阅读声明:本文档仅供学习,由于个人能力有限,文档中有错漏的地方还请指出,大家共同学习. 目前在学习怎么样写jquery,模仿阶段,有兴趣的同学可以和我一起学习,共同交流,在学习的路上希望有你做伴. 在 ...
- 【前端性能】必须要掌握的原生JS实现JQuery
很多时候,我们经常听见有人说jquery有多快多快.在这个各种类库满天飞的时候,不得不说的是,能有原生JS快吗? 是的,明显原生JS要更快,因为诸如JQuery这样的库必须要兼容各种浏览器和低版本和许 ...
- 原生js和jquery实现图片轮播特效
本文给大家分享的是使用原生JS和JQ两种方法分别实现相同的图片轮播特效,十分的实用,也非常方便大家对比学习原生js和jQuery,有需要的小伙伴可以参考下. 1)首先是页面的结构部分对于我这种左右切换 ...
- 原生js和jquery实现图片轮播特效(转)
本文给大家分享的是使用原生JS和JQ两种方法分别实现相同的图片轮播特效,十分的实用,也非常方便大家对比学习原生js和jQuery,有需要的小伙伴可以参考下. 1)首先是页面的结构部分对于我这种左右切换 ...
- 原生JS和JQuery的区别
1.原生js和jQuery的入口函数加载模式不同 原生js等页面dom加载完成并且图片等资源也加载完成之后才会执行: jQuery则是等页面dom加载完成执行,不会等图片等资源也加载完成: (也就是说 ...
- onload事件与ready事件的区别,原生js与jquery的区别
onload事件与ready事件分别是原生js与jquery的入口函数 原生js入口函数写法: window.onload=function(){ } jquery入口函数写法: $(document ...
- JavaScript中函数和类(以及this的使用<重点>,以及js和jquery讲解,原生js实现jquery)
1.javascript中以函数来表示类: 一般函数是小写开头:function foo() 类开头是大写:function Foo() 实例化类: obj = new Foo() 其他属性就同类是一 ...
随机推荐
- Codeforces 1023 C.Bracket Subsequence-STL(vector) (Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Fi)
C. Bracket Subsequence ... 代码: 1 //C 2 #include<iostream> 3 #include<cstdio> 4 #include& ...
- HDU 1754.I Hate It-结构体版线段树(单点更新+区间查询最值)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 在ubuntu下面为php添加redis扩展
首先下载redis扩展:wget https://github.com/nicolasff/phpredis/zipball/master -o php-redis.zip 解压缩:unzip php ...
- CF 1006B Polycarp's Practice【贪心】
Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a1,a ...
- 奶牛与农夫John与oj
当蒟蒻的我悲惨的发现oj出现大量的奶牛与农夫时,觉得早晚usaco要占领oj,于是绝望的开始记录农夫与奶牛的题目……. 一道usaco月赛的题…在oj用作练习二维数组,虽然数据的大量字符确实很让人不爽 ...
- Codeforces #428 Div2 D
#428 Div2 D 题意 给出一些数,现在要求找出一些数满足 \(i_1 < i_2 < i_3 < ... < i_k\) 以及 \(gcd(a_{i_1}, a_{i_ ...
- C++ 对象的内存布局【转】
单一继承: 虚函数表在最前面的位置. 成员变量根据其继承和声明顺序依次放在后面. 在单一的继承中,被overwrite的虚函数在虚函数表中得到了更新. 多重继承 每个父类都有自己的虚表. 子类的成员函 ...
- 1、Flask实战第1天:第一个Flask程序
Flask是流行的python web框架...(* ̄︶ ̄) 零基础到企业级论坛实战,人生苦短,我用python,开启FLask之旅吧... 安装开发环境 下载Python win版安装包 双击运行, ...
- 什么是EPEL 及 Centos上安装EPEL(转)
什么是EPEL 及 Centos上安装EPEL 转自:http://www.unxmail.com/read.php?67 RHEL以及他的衍生发行版如CentOS.Scientific Linux为 ...
- spring boot 2.x静态资源会被HandlerInterceptor拦截的原因和解决方法
在spring boot 1.5.x中,resources/static目录下的静态资源可以直接访问,并且访问路径上不用带static,比如静态资源放置位置如下图所示: 那么访问静态资源的路径可以是: ...