//addLoadEvent,向系统内置的window.onLoad函数,添加处理事件
function addLoadEvent(func){ var oldOnLoad=window.onload; if(typeof window.onload!="function"){ window.onload=func; }else{ window.onload=function(){ oldOnLoad(); func(); } } }
//insertAfter,向目标元素targetElement后增添一个新元素newElement
function insertAfter(newElement,targetElement){ var parent=targetElement.parentNode; if(parent.lastChild==targetElement){ parent.appendChild(newElement); }else{ parent.insertBefore(newElement,targetElement.nextSibling); } }
//GetHttpObject,获取ajax中的XMLHttpRequest对象,保证各个浏览器的兼容
//获取ajax中的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(); }
//getNextElement,获取对应元素的下一个节点元素
function getNextElement(node){ if(node.nextSibling.nodeType==1){ return node.nextSibling; }else{ return getNextElement(node.nextSibling); } }
//getLastChildElement,获取最后一个孩子节点元素
function getLastElementChild(node){ if(node.lastChild.nodeType==1){ return node.lastChild; } var childrenNode=node.getElementsByTagName("*"); if(childrenNode.length<1){return;} return childrenNode[childrenNode.length-1]; }
//addClass 为元素增添特定样式类
function addClass(node,className){ var oldClass=node.getAttribute("class"); if(!oldClass){ node.setAttribute("class",oldClass); }else{ var newClass=oldClass+" "+className; node.setAttribute("class",newClass); } }
//styleElementsSibling 为特定元素的下一个相邻元素添加样式
function styleElementsSibling(tag,theClass){ var elems=document.getElementsByTagName(tag); for(var i=0;i<elems.length;i++){ var eleSibling=getNextElement(elems[i]); eleSibling.addClass(theClass); }
//moveElement 为元素添加运动的动画效果
function moveElement(elementId,final_x,final_y,interval){ var elem=document.getElementById(elementId); if(elem.movement!=null){ clearTimeout(elem.movement); } if(!elem.style.top){ elem.style.top="0px"; } if(!elem.style.left){ elem.style.left="0px"; } var xPos=elem.style.left; var yPos=elem.style.top; var dist=0; var xPos=parseInt(elem.style.left); var yPos=parseInt(elem.style.top); if(xPos<final_x){ dist=Math.ceil((final_x-xPos)/10); xPos=xPos+dist; } if(xPos>final_x){ dist=Math.ceil((xPos-final_x)/10); xPos=xPos-dist; } if(yPos<final_y){ dist=Math.ceil((final_y-yPos)/10); yPos=yPos+dist; } if(yPos>final_y){ dist=Math.ceil((yPos-final_y)/10); yPos=yPos-dist; } elem.style.left=xPos+"px"; elem.style.top=yPos+"px"; var repeat="moveElement("+elementId+","+final_x+","+final_y+","+interval+")"; elem.movement=setTimeout(repeat,interval); }