本文介绍了运行时错误......无法弄清楚为什么......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 您好。 这是我读过的一本书中的参考文件,其中核心主题是''事件听众'使用的'' 。 我正在尝试在Firefox中加载文件,它给我一个错误信息: 行:5 字符:1 错误:''文档''未定义 代码:800A1391 来源:微软JScript运行时错误 我需要这个文件才能工作,因为它是我所有编码的参考文件。 这里的内容是这里的文件。 var Core = {}; // W3C DOM 2活动模型 if(document.addEventListener) { Core.addEventListener = function(target,type,listener) { target.addEventListener(type,listener,false); }; Core.removeEventListener = function(target,type,listener) { target.removeEventListener(类型,监听器,假); }; Core.preventDefault =功能(事件) { event.preventDefault(); }; Core.stopPropagation = function(event) { event.stopPropagation(); }; } // Internet Explorer事件模型 else if(document.attachEvent) { Core.addEventListener = function(target,type,listener) { //阻止两次添加相同的侦听器,因为DOM 2 //事件会忽略这样的重复 if(Core._findListener(target,type,listener)!= -1) return; // listener2以两种方式调用侦听器作为目标的方法,取决于这个版本的IE支持, //并将全局事件对象作为参数传递 var listener2 = function() { var event = window.event; if(Function.prototype.call) { listener.call(target,event ); } else { target._currentListener = listener; target._currentListener(event) target._currentListener = null; } }; //使用IE的attachEvent方法添加listener2 target.attachEvent(" on" + type,listener2); //创建一个描述这个监听器的对象,这样我们可以 //稍后清理它 var listenerRecord = { 目标:目标, 类型:类型, listener:listener, listener2:listener2 }; //获取对包含窗口对象的引用目标 var targetDocument = target.document || target; var targetWindow = targetDocument.parentWindow; //为这个听众创建一个唯一的ID var listenerId =" 1" + Core._listenerCounter ++; //在窗口对象中存储此监听器的记录 if(!targetWindow._allListeners) targetWindow._allListeners = {}; targetWindow._allListeners [listenerID] = listenerRecord; //存储此监听器's目标中的ID if(!target._listeners)target._listeners = []; target._listeners [target._listeners.length] = listenerId; //设置Core._removeAllListeners来清理所有 //卸载时的听众 if(!targetWindow._unloadListenerAdded) { targetWindow._unloadListenerAdded = true; targetWindow.attachEvent( " onunload",Core._removeAllListeners); } }; Core.removeEventListener = function(target,type,listener) ) { //找出听众是否真的被添加到目标 var listener Index = Core._findListener( target,type,listener); if(listenerIndex == -1)return; //获取对包含目标的窗口对象的引用 var targetDocument = target.document || target; var targetWindow = targetDocument.parentWindow; //从窗口对象获取监听器的记录 var listenerId = target._listeners [listenerIndex]; var listenerRecord = targetWindow._allListeners [listenerId]; //删除侦听器,并从目标中删除其ID target.detachEvent(" on" + type,listenerRecord.listener2); target ._listeners.splice(listenerIndex,1); //从窗口对象中删除监听器的记录 delete targetWindow._allListeners [listenerId]; }; Core.preventDefault =功能(事件) { event.returnValue = false; }; Core.stopPropagation = function(event) { event.cancelBubble = true; }; Core._findListener = function(target,type,listener) { //获取添加到目标的侦听器ID数组 var listeners = target._listeners; if(!听众)返回-1; //获取对包含目标的窗口对象的引用 var targetWindow = targetDocument.parentWindow; //向后搜索(加快onunload处理), //找到听众 for(var i = listeners.length - 1; i> = 0; i--) { //从目标获取听众的身份 var listenerId =听众[i]; //从窗口对象获取监听器的记录 var listenerRecord = targetWindow._allListeners [listenerId]; //将类型和监听器与检索到的记录进行比较 if(listenerRecord.type == type&& listenerRecord.listener ==听众) { 返回i; } } 返回-1; }; Core._removeAllListeners = function() { var targetWindow = this; for(id in targetWindow._allListeners) { var li stenerRecord = targetWindow._allListeners [id]; listenerRecord.target.detachEvent( " on" + listenerRecord.type,listenerRecord.listener2); 删除targetWindow._allListeners [id]; } }; Core._listenerCounter = 0; } Core.addClass = function(target,theClass) { if(!Core.hasClass(target,theClass)) { if(target.className =="" ;) { target.className = theClass; } else { target.className + =" " + theClass; } } }; Core.getElementsByClass = function(theClass ) { var elementArray = []; if(typeof document.all!=" undefined") { elementArray = document.all; } 其他 { elementArray = document.getElementsByTagName(" *"); } var matchedArray = [] ; var pattern = new RegExp("(^ |)" + theClass +"(| $)"); for( var i = 0; i< elementArray.length; i ++) { if(pattern.test(elementArray [i] .className)) { matchedArray [matchedArray.length] = elementArray [i]; } } 返回matchedArray; }; Core.hasClass = function(target,theClass) { var pattern = new RegExp("(^ |)" + theClass +"(| $)"); if(pattern.test(target.className)) { 返回true; } 返回false; }; Core.removeClass = function(target,theClass) { var pattern = new RegExp("(^ | )" + theClass +"(| $)"); target.className = target.className.replace(pattern," $ 1"); target.className = target.className.replace(/ $ /,""); }; Core.getComputedStyle = function(element,styleProperty) { var computedStyle = null; if(typeof element.currentStyle!=" undefined") { computedStyle = element.currentStyle; } else { computedStyle = document.defaultView.getComputedStyle(element,null); } return computedStyle [styleProperty]; }; Core.start = function(runnable) { Core.addEventListener(window," load",runnable.init); }; - 通过WebmasterKB.com发布消息 http://www.webmasterkb.com/Uwe/Forum...cript/200808/1Hello.This is a reference file from a book I read in which the core subject is theuse of ''event listeners''.I''m trying to load the file in Firefox and it''s giving me an error message:Line: 5Char: 1Error: ''document'' is undefinedCode: 800A1391Source: Microsoft JScript Runtime ErrorI need this file to work because it''s the reference file for all of my coding.Here''s the contents of the file here.var Core={};// W3C DOM 2 Events modelif (document.addEventListener){Core.addEventListener = function(target, type, listener){target.addEventListener (type, listener, false);};Core.removeEventListener = function(target, type, listener){target.removeEventListener(type, listener, false);};Core.preventDefault = function(event){event.preventDefault();};Core.stopPropagation = function(event){event.stopPropagation();};}// Internet Explorer Events modelelse if (document.attachEvent){Core.addEventListener = function(target, type, listener){// prevent adding the same listener twice, since DOM 2// Events ignores duplicates like thisif (Core._findListener(target, type, listener) != -1)return;// listener2 calls listener as a method of target in one of// two ways, depending on what this version of IE supports,// and passes it the global event object as an argumentvar listener2 = function(){var event = window.event;if (Function.prototype.call){listener.call(target, event);}else{target._currentListener = listener;target._currentListener(event)target._currentListener = null;}};// add listener2 using IE''s attachEvent methodtarget.attachEvent("on" + type, listener2);// create an object describing this listener so we can// clean it up latervar listenerRecord ={target: target,type: type,listener: listener,listener2: listener2};// get a reference to the window object containing targetvar targetDocument = target.document || target;var targetWindow = targetDocument.parentWindow;// create a unique ID for this listenervar listenerId = "1" + Core._listenerCounter++;// store a record of this listener in the window objectif (!targetWindow._allListeners)targetWindow._allListeners = {};targetWindow._allListeners[listenerID] = listenerRecord;// store this listener''s ID in targetif (!target._listeners) target._listeners = [];target._listeners[target._listeners.length] = listenerId;// set up Core._removeAllListeners to clean up all// listeners on unloadif (!targetWindow._unloadListenerAdded){targetWindow._unloadListenerAdded = true;targetWindow.attachEvent("onunload", Core._removeAllListeners);}};Core.removeEventListener = function(target, type, listener){// find out if the listener was actually added to targetvar listenerIndex = Core._findListener(target, type, listener);if (listenerIndex == -1) return;// get a reference to the window object containing targetvar targetDocument = target.document || target;var targetWindow = targetDocument.parentWindow;// obtain the record of the listener from the window objectvar listenerId = target._listeners[listenerIndex];var listenerRecord =targetWindow._allListeners[listenerId];// remove the listener, and remove its ID from targettarget.detachEvent("on" + type, listenerRecord.listener2);target._listeners.splice(listenerIndex, 1);// remove the record of the listener from the window objectdelete targetWindow._allListeners[listenerId];};Core.preventDefault = function(event){event.returnValue = false;};Core.stopPropagation = function(event){event.cancelBubble = true;};Core._findListener = function(target, type, listener){// get the array of listener IDs added to targetvar listeners = target._listeners;if (!listeners) return -1;// get a reference to the window object containing targetvar targetDocument = target.document || target;var targetWindow = targetDocument.parentWindow;// searching backward (to speed up onunload processing),// find the listenerfor (var i = listeners.length - 1; i >= 0; i--){// get the listener''s ID from targetvar listenerId = listeners[i];// get the record of the listener from the window objectvar listenerRecord =targetWindow._allListeners[listenerId];// compare type and listener with the retrieved recordif (listenerRecord.type == type &&listenerRecord.listener == listener){return i;}}return -1;};Core._removeAllListeners = function(){var targetWindow = this;for (id in targetWindow._allListeners){var listenerRecord = targetWindow._allListeners[id];listenerRecord.target.detachEvent("on" + listenerRecord.type, listenerRecord.listener2);delete targetWindow._allListeners[id];}};Core._listenerCounter = 0;}Core.addClass = function(target, theClass){if (!Core.hasClass(target, theClass)){if (target.className == " "){target.className = theClass;}else{target.className += " " + theClass;}}};Core.getElementsByClass = function(theClass){var elementArray = [];if (typeof document.all != "undefined"){elementArray = document.all;}else{elementArray = document.getElementsByTagName("*");}var matchedArray = [];var pattern = new RegExp("(^| )" + theClass + "( |$)");for (var i = 0; i < elementArray.length; i++){if (pattern.test(elementArray[i].className)){matchedArray[matchedArray.length] = elementArray[i];}}return matchedArray;};Core.hasClass = function(target, theClass){var pattern = new RegExp("(^| )" + theClass + "( |$)");if (pattern.test(target.className)){return true;}return false;};Core.removeClass = function(target, theClass){var pattern = new RegExp("(^| )" + theClass + "( |$)");target.className = target.className.replace(pattern, "$1");target.className = target.className.replace(/ $/, " ");};Core.getComputedStyle = function(element, styleProperty){var computedStyle = null;if (typeof element.currentStyle != "undefined"){computedStyle = element.currentStyle;}else{computedStyle =document.defaultView.getComputedStyle(element, null);}return computedStyle[styleProperty];};Core.start = function(runnable){Core.addEventListener(window, "load", runnable.init);};--Message posted via WebmasterKB.com http://www.webmasterkb.com/Uwe/Forum...cript/200808/1推荐答案 这篇关于运行时错误......无法弄清楚为什么......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-25 03:57