Javascript代码片段
1 <script type="text/javascript"> 2 /*getElementsByClassName*/ 3 function getElementsByClassName(node,className){ 4 if(node.getElementsByClassName){ 5 return node.getElementsByClassName(className); 6 } else { 7 var results=new Array(); 8 var elems=node.getElementsByTagName("*"); 9 for(var i=0;i<elems.length;i++){ 10 if(elems[i].className.indexOf(className)!=-1){ 11 results[results.length]=elems[i]; 12 } 13 } 14 return results; 15 } 16 } 17 </script>
共享onload事件
function addLoadEvent(func){ var oldonload=window.onload; if(typeof window.onload != "function"){ window.onload=func; } else { window.onload=function(){ oldonload(); func(); } } }
insertAfter(newElement,targetElement)
function insertAfter(newElement,targetElement){ var parent=targetElement.parentNode; if(parent.lastChild==targetElement){ parent.appendChild=newElement; } else { parent.insertBefore(newElement,targetElement.nextSibling); } }
初始化XMLHttpRequest对象
function getHTTPObject(){ if(typeof XMLHttpRequest =="undefined"){ XMLHttpRequest=function(){ try{ return new ActiveXObject("Msxml2.XMLHTTP.6.0");} catch(e){} try{ return new ActiveXObject("Msxml2.XMLHTTP.3.0");} catch(e){} try{ return new ActiveXObject("Msxml2.XMLHTTP");} catch(e){} return false; } } return new XMLHttpRequest(); }
addClass
function addClass(elem,className){ if(!elem.className){ elem.className=className; } else { var newClassName=elem.className; newClassName+=" "; newClassName+=className; elem.className=newClassName; } }
fadeIn、fadeOut、setOpacity
1 /** 2 *@name:setOpacity 3 *@description:设置元素的透明度 4 *@arguments:elem==>元素,opacity==>目标透明度 5 */ 6 7 function setOpacity(elem,opacity){ 8 if(elem.filters){//IE透明滤镜 9 elem.style.filter="alpha(opacity="+opacity+")"; 10 } else { 11 elem.style.opacity=opacity/100; 12 } 13 } 14 15 16 /** 17 *@name:fadeIn 18 *@description:淡入效果 19 *@arguments:elem==>元素,speed==>速度(毫秒),opacity==>目标透明度 20 */ 21 function fadeIn(elem,speed,opacity){ 22 var peed=speed || 20; 23 var opacity=opacity || 100; 24 //清除真在执行的fade 25 if(elem.fade){ 26 clearTimeout(elem.fade); 27 } 28 //对要处理的元素初始化 29 elem.style.display="block"; 30 31 //初始化透明度值0 32 var val=0; 33 //循环改变透明值直到透明值等于目标透明值停止,每次增加剩余的1/10 34 (function(){ 35 setOpacity(elem,val);//调用设置透明度的函数 36 val+=Math.ceil((opacity-val)/10); 37 if(val<opacity){ 38 elem.fade=setTimeout(arguments.callee,speed); //调用自身来不断改变透明值并不断调用setOpacity来设置新的透明值 39 } 40 })(); 41 } 42 43 /** 44 *@name:fadeOut 45 *@description:淡出效果 46 *@arguments:elem==>元素,speed==>速度(毫秒),opacity==>目标透明度 47 */ 48 49 function fadeOut(elem,speed,opacity){ 50 var speed=speed || 20; 51 var opacity=opacity || 0; 52 //清除真在执行的fade 53 if(elem.fade){ 54 clearTimeout(elem.fade); 55 } 56 //初始化透明值 57 var val=100; 58 //循环改变透明值直到透明值等于目标透明值停止,每次减少剩余的1/10 59 (function(){ 60 setOpacity(elem,val);//调用设置透明度的函数 61 val-=Math.ceil((val-opacity)/10); 62 if(val>opacity){ 63 elem.fade=setTimeout(arguments.callee,speed); //调用自身来不断改变透明值并不断调用setOpacity来设置新的透明值 64 } else if(val<=0){ 65 elem.style.display="none";//透明值达到目标值后隐藏 66 } 67 })(); 68 }
js动态加载
1 var JsLoader = {
2 load: function(sUrl, fCallback) {
3 var _script = document.createElement("script");
4 _script.setAttribute("type", "text/javascript");
5 _script.setAttribute("src", sUrl);
6 document.getElementsByTagName("head")[0].appendChild(_script);
7
8 if (/msie/.test(window.navigator.userAgent.toLowerCase())) {
9 _script.onreadystatechange = function() {
10 if (this.readyState == "loaded" || this.readyState == "complete") {
11 fCallback();
12 }
13 };
14 } else if (/gecko/.test(window.navigator.userAgent.toLowerCase())) {
15 _script.onload = function() {
16 fCallback();
17 };
18 } else {
19 fCallback();
20 }
21 }
22 }
2 load: function(sUrl, fCallback) {
3 var _script = document.createElement("script");
4 _script.setAttribute("type", "text/javascript");
5 _script.setAttribute("src", sUrl);
6 document.getElementsByTagName("head")[0].appendChild(_script);
7
8 if (/msie/.test(window.navigator.userAgent.toLowerCase())) {
9 _script.onreadystatechange = function() {
10 if (this.readyState == "loaded" || this.readyState == "complete") {
11 fCallback();
12 }
13 };
14 } else if (/gecko/.test(window.navigator.userAgent.toLowerCase())) {
15 _script.onload = function() {
16 fCallback();
17 };
18 } else {
19 fCallback();
20 }
21 }
22 }