Javascript:IE event.preventDefault 为 null 或不是对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3677748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:54:46  来源:igfitidea点击:

Javascript: IE event.preventDefault is null or not an object

javascriptinternet-explorer

提问by avrx

My code works fine in FF/Chrome/Everything except IE(failed in both 7/8, didn't bother going furthur down). Due to restrictions, I cannot use jQuery and am hard-coding the Javascript. The issue is with IE, I am getting "preventDefault is null or not an object". Hoping one of you has the answer, and here's relevant code:

我的代码在 FF/Chrome/除 IE 之外的所有东西中都可以正常工作(在 7/8 中都失败了,没有打扰继续下去)。由于限制,我无法使用 jQuery 并且正在对 Javascript 进行硬编码。问题出在 IE 上,我收到“preventDefault 为 null 或不是对象”。希望你们中的一个人有答案,这是相关的代码:

AddEvent Method:

添加事件方法:

function addEvent( obj, type, fn ) {  
  if ( obj.attachEvent ) {  
    obj['e'+type+fn] = fn;  
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}  
    obj.attachEvent( 'on'+type, obj[type+fn] );  
  } else  
    obj.addEventListener( type, fn, false );  
};

Event handler throwing error:

事件处理程序抛出错误:

function mousedown(e){  
  if(e.preventDefault){  
    e.preventDefault();  
  } else {  
    e.returnValue = false;  
    e.cancelBubble=true;  
  }
  //Processing   
};

Also DOCTYPE and charset are both set on the calling HTML page. Any ideas would be appreciated. The error is thrown in the if statement for the mousedown method.

此外,DOCTYPE 和字符集都在调用 HTML 页面上设置。任何想法,将不胜感激。该错误在 mousedown 方法的 if 语句中抛出。

EDIT:

编辑:

Due to the fact that grabbing window.event did "fix" the main issue, I discovered the problem was a different section of code. Basically I am adding a element ontop of a pre-placed element, and then registering mousedown/mousemove events to that div. When firing the event on the <div>, THAT'S where the error is thrown.

由于抓取 window.event 确实“修复”了主要问题,我发现问题出在不同的代码部分。基本上,我在预先放置的元素上添加一个元素,然后将 mousedown/mousemove 事件注册到该 div。在 上触发事件时<div>,这就是引发错误的地方。

Could this be something due to the fact that I have events registered to 2

这可能是因为我将事件注册到 2

rect = document.createElement('div');  
rect.className='square';  
rect.id='chooser_rectangle';  
rect.style.left=initx+'px';  
rect.style.top=inity+'px';  

addEvent(rect,"mousemove",mousemove);  
addEvent(rect,"mousedown",mousedown);  

回答by slebetman

In IE the event object is not passed as the first argument to an event handler. Instead it is a global variable:

在 IE 中,事件对象不会作为第一个参数传递给事件处理程序。相反,它是一个全局变量:

function mousedown(e){
 var evt = e || window.event; // IE compatibility
 if(evt.preventDefault){  
  evt.preventDefault();  
 }else{  
  evt.returnValue = false;  
  evt.cancelBubble=true;  
 }
 //Processing   
};

回答by Spudley

IE has a different event model to other browsers (even IE8). In IE you would call this to do the same thing:

IE 具有与其他浏览器(甚至 IE8)不同的事件模型。在 IE 中,你会调用它来做同样的事情:

event.returnValue = false;

You need to determine what the browser supports and call the correct method. So first check if event.returnValue is set, if so then call it, otherwise call preventDefault().

您需要确定浏览器支持什么并调用正确的方法。所以首先检查 event.returnValue 是否设置,如果设置则调用它,否则调用 preventDefault()。