js 阻止事件冒泡 支持所有主流浏览器




  1. function getEvent(){
  2. if(window.event) {return window.event;}
  3. func=getEvent.caller;
  4. while(func!=null){
  5. var arg0=func.arguments[0];
  6. if(arg0){
  7. if((arg0.constructor==Event || arg0.constructor ==MouseEvent
  8. || arg0.constructor==KeyboardEvent)
  9. ||(typeof(arg0)=="object" && arg0.preventDefault
  10. && arg0.stopPropagation)){
  11. return arg0;
  12. }
  13. }
  14. func=func.caller;
  15. }
  16. return null;
  17. }
  18. //阻止事件冒泡
  19. function stopEvent(){
  20. try{
  21. (window.event.cancelBubble = true)
  22. }catch(e){
  23. getEvent().stopPropagation()
  24. }
  25. }

原文地址:https://www.cnblogs.com/signheart/p/6598164.html