var userAgent = navigator.userAgent.toLowerCase(); var is_opera = userAgent.indexOf('opera') != -1 && opera.version(); // Opera var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3); // Firefox var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3); // IE var is_mac = userAgent.indexOf('mac') != -1; // Mac var oEvent = getEvent(); // 获取事件event function getEvent() { return window.event ? window.event : event; } // 兼容ie的outerHTML,FF中没有outerHtml的方法。 if (window.HTMLElement) { HTMLElement.prototype.__defineSetter__("outerHTML", function(sHTML) { var r = this.ownerDocument.createRange(); r.setStartBefore(this); var df = r.createContextualFragment(sHTML); this.parentNode.replaceChild(df, this); return sHTML; }); HTMLElement.prototype.__defineGetter__("outerHTML", function() { var attr; var attrs = this.attributes; var str = "<" + this.tagName.toLowerCase(); for (var i = 0; i < attrs.length; i++) { attr = attrs[i]; if (attr.specified) str += " " + attr.name + '="' + attr.value + '"'; } if (!this.canHaveChildren) return str + ">"; return str + ">" + this.innerHTML + "</" + this.tagName.toLowerCase() + ">"; }); HTMLElement.prototype.__defineGetter__("canHaveChildren", function() { switch (this.tagName.toLowerCase()) { case "area": case "base": case "basefont": case "col": case "frame": case "hr": case "img": case "br": case "input": case "isindex": case "link": case "meta": case "param": return false; } return true; }); } // 获取event.x function eventX() { return oEvent.x ? oEvent.x : oEvent.pageX; } // 获取event.x function eventY() { return oEvent.y ? oEvent.y : oEvent.pageY; } // 获取事件对象 function eventSrcElement() { return oEvent.srcElement ? oEvent.srcElement : oEvent.target; }