JS中的兼容性问题

事件对象兼容
       window.event只能在IE下运行,而不能在Firefox下运行,这是因为Firefox的event只能在事件发生的现场使用。Firefox必须从源处加入event作参数传递。IE忽略该参数,用window.event来读取该event。
解决的方法:
       event = event || window.event
阻止事件冒泡兼容
  stopPropagation()和cancelBubble,前者是方法,是标准的写法,后者是属性,赋值true表示阻止,是IE的写法。
解决方法:
  判断stopPropagation是否存在,如果存在则用标准写法否则则用IE的写法,不可反过来判断。
  event.stopPropagation ? event.stopPropagation() : event.cancelBubble=true;
阻止默认行为兼容
  preventDefault() 和 returnValue()
解决方法:
     event.preventDefault?event.preventDefault():event.returnValue=false;
  return false;

猜你喜欢

转载自www.cnblogs.com/gsdnb/p/11752531.html