if-else 兼容小集合

1.获取非行间样式

  function getStyle (obj,name){

    if(obj.currentStyle){

      return obj.currentStyle[name]; //IE

    }

    else{

      return getComputedStyle(obj,false)[name];  //标准(Firefox,Chrome)

    }

  }

2.查找节点

  if(obj.firstElementChild){

    obj.firstElementChild.style.background = 'green'; //IE6-8外使用

  }

  else{

    obj.firstChild.style.background = 'green'; //IE6-8

  }

相似用法:lastElementChild-lastChild;

     previousElementSibling-previousSibling;

     nextElementSibling-nextSibling;

3.透明度

  if(attr=='opacity'){    //attr为属性名,IE

    //获取attr值cur

      //cur = Math.around(parsrFloat(getStyle(obj,attr))*100);

    //设置透明度

    obj.style.filter = 'alpha(opacity:'+cur+')';

    obj.style.opacity = cur/100;

  }

  else{

    //获取,标准DOM

    //cur = parseInt(getStyle(obj,attr));

    //设置

    obj.style[attr] = cur +'px';

  }

4.创建Ajax

  if(window.XMLHttpRequest){

    oAjax = new XMLHttpRequest(); //标准DOM

  }

  else{

    oAjax = new ActiveXObject("Microsoft.XMLHTTP"); //IE

  }

5.事件与目标

  oEvent = ev||event;

  o = event.target||event.srcElement;

6.事件监听

  if(obj.attachEvent){

    obj.attachEvent('on'+oEvent,function(){ //IE

      //do something......

    }); 

  }

  else{

    obj.addEventListener(oEvent,function(ev){  //do something......},false); //标准DOM

  }

7.移除事件

  if(element.removeEventListener){ //标准DOM

    element.removeEventListener(oEvent);

  }

  else{

    element.detachEvent('on'+oEvent);  //IE

  }

8.阻止默认

  if(oEvent.preventDefault){

    oEvent.preventDefault(); //标准DOM

  }

  else{

    oEvent.returnValue = false;  //IE

  }

9.阻止冒泡

  if(oEvent.stopPropagation){

    oEvent.stopPropagation(); //标准DOM

  }

  else{

    oEvent.cancelBubble = true;  //IE

  }

......

 

posted @ 2014-04-12 09:33  Pada  阅读(156)  评论(0编辑  收藏  举报