惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
Stack Overflow Blog
Stack Overflow Blog
人人都是产品经理
人人都是产品经理
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Vercel News
Vercel News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Y
Y Combinator Blog
The Cloudflare Blog
Last Week in AI
Last Week in AI
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
Google DeepMind News
Google DeepMind News
D
DataBreaches.Net
博客园 - 司徒正美
B
Blog RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
L
LangChain Blog

博客园 - Eric.Wang

jQuery的.bind()、.live()和.delegate()之间区别 [转][探讨]为什么说JavaScript是性价比最高的技术? [转载]C# 中的委托和事件 常见兼容性问题及解决方案 浅析JavaScript继承方式 深入理解js闭包 兼容IE和FF的js脚本做法(比较常用) CSS的盒子模型(Box Model) jQuery使用技巧 区分IE6,IE7,firefox三种浏览器的CSS HACK CSS基础学习 IE与Firefox对CSS的解析差异 关于JavaScript中apply与call的用法意义及区别 JS调用后台方法大全 Aptana配置说明 Asp.Net收集 VS2005打开js文件,中文都是乱码 检测远程URL是否存在的三种方法 - Eric.Wang - 博客园 鼠标停留在GridView某行的颜色改变 - Eric.Wang - 博客园
一些常用且实用的原生 JavaScript函数
Eric.Wang · 2010-09-08 · via 博客园 - Eric.Wang

一些常用且实用的原生 JavaScript函数 日常开始中常用到的一些原生JavaScript函数,比较实用, 今天特地整理一下,分享给大家,希望对大家有用,会常更新,同时也欢迎大家补充. css及html方面的技巧总结,点此前往: 前端开发中一些常用技巧总结.

1. document.getElementById的简写: http://mrthink.net/javascript-getbyid-simplewrite/;

2. getElementsByTagName的简写方式: http://mrthink.net/javascrip-simple-getelementsbytagname/;

3. 原生JavaScript中获取元素索引的函数: http://mrthink.net/javascript-index-functio/;

 4. 替代window.onload,可多次调用的加载函数:

function iLoad(func) { var oLoad=window.onload; if(typeof window.onload!='function'){ window.onload=func; }else{ window.onload=function(){ oLoad(); func(); } } }

5. 获取下一个元素节点:

function nextElem(node){ if(node.nodeType==1) return node; if(node.nextSibling) return nextElem(node.nextSibling); return null; }

 6. 获取上一个元素节点(此函数须调用第五条中的函数):

 function prevElem(node){ if(node.nodeType==1){ return node; }else if(node.previousSibling){ return nextElem(node.previousSibling); }else{ return null; } }

7. 原生JavaScript中有insertBefore方法,可惜却没有insertAfter方法,怎么办?用如下函数实现:

function insertAfter(newChild,refChild){ var parElem=refChild.parentNode; if(parElem.lastChild==refChild){ refChild.appendChild(newChild); }else{ parElem.insertBefore(newChild,refChild.nextSibling); } }

8. 为元素添加样式[记住是添加不是替换,相当于jQuery中的addClass(value)]:

 function addClass(elem,value){ if(!elem.className){ elem.className=value; }else{ var oValue=elem.className; oValue+=" "; oValue+=value; elem.className=oValue; } }

 9. 获取元素的样式:

function getStyle(id,stylename){ var elem=$(id); var realStyle=null; if(elem.currentStyle){ realStyle=elem.currentStyle[stylename]; }else if(window.getComputedStyle){ realStyle=window.getComputedStyle(elem,null)[stylename]; } return realStyle; }

 9. 兼容事件绑定: function addEventSamp(obj,evt,fn){ if (obj.addEventListener) { obj.addEventListener(evt, fn, false); }else if(obj.attachEvent){ obj.attachEvent('on'+evt,fn); } }

10. 移除事件 function removeEventSamp(obj,evt,fn){ if(obj.removeEventListener){ obj.removeEventListener(evt,fn,false); }else if(obj.detachEvent){ obj.detachEvent('on'+evt,fn); } }