Mozilla中独有的读写器(defineGetter、defineSetter)以及可以给Element,Event等加上prototype原型,使得在IE里用的方法同样在Mozilla中可以适用,下面贴出常用的一些代码
例如
obj.insertAdjacentHTML, currentStyle, obj.attachEvent, obj.detachEvent等等。
版权属于Erik Arvidsson, webfx
代码如下:if (Browser.isMozilla) { // set up ie environment for Moz
extendEventObject();
emulateAttachEvent();
emulateEventHandlers(["click", "dblclick", "mouseover", "mouseout",
"mousedown", "mouseup", "mousemove",
"keydown", "keypress", "keyup"]);
emulateCurrentStyle();
/*emulateDocumentAll();
emulateElement()
*/
// It is better to use a constant for event.button
Event.LEFT = 0;
Event.MIDDLE = 1;
Event.RIGHT = 2;
}
else {
Event = {};
// IE is returning wrong button number
Event.LEFT = 1;
Event.MIDDLE = 4;
Event.RIGHT = 2;
}
/*
* Extends the event object with srcElement, cancelBubble, returnValue,
* fromElement and toElement
*/
function extendEventObject() {
Event.prototype.__defineSetter__("returnValue", function (b) {
if (!b) this.preventDefault();
return b;
});
Event.prototype.__defineSetter__("cancelBubble", function (b) {
if (b) this.stopPropagation();
return b;
});
Event.prototype.__defineGetter__("srcElement", function () {
var node = this.target;
while (node.nodeType != 1) node = node.parentNode;
return node;
});
Event.prototype.__defineGetter__("fromElement", function () {
var node;
if (this.type == "mouseover")