FF/IE 兼容问题整理
1.e.target || e.srcElement;
if (document.all) e = event;
obj = e.target || e.srcElement;
2.event.keyCode || e.which
$("#id").keydown(function(e) {
var key = window.event ? event.keyCode : e.which;
key == 13 && $("#Login").click();
});
3.obj.parentNode || obj.parentElement
function subtrace(e) {
var obj = e.target || e.srcElement;
var parentObj = obj.parentNode || obj.parentElement;
}
3.attachEvent||addEventListener
if (IsIE){
document.body.attachEvent("onclick",CloseSmileyPanel);
}
else{
document.body.addEventListener("click",CloseSmileyPanel,true);
}
4.filters.alpha.opacity
if (navigator.userAgent.toLowerCase().indexOf("msie") != -1) {
var f = Drag.tdiv.filters.alpha.opacity;
}
else {
var f = Drag.tdiv.opacity;
}
5.outerHTML
方法一:
<head>
<SCRIPT LANGUAGE="JavaScript">
<!--
if(typeof(HTMLElement)!="undefined" && !window.opera)
{
HTMLElement.prototype.__defineGetter__("outerHTML",function()
{
var a=this.attributes, str="<"+this.tagName, i=0;for(;i<a.length;i++)
if(a[i].specified) str+=" "+a[i].name+'="'+a[i].value+'"';
if(!this.canHaveChildren) return str+" />";
return str+">"+this.innerHTML+"</"+this.tagName+">";
});
HTMLElement.prototype.__defineSetter__("outerHTML",function(s)
{
var d = document.createElement("DIV"); d.innerHTML = s;
for(var i=0; i<d.childNodes.length; i++)
this.parentNode.insertBefore(d.childNodes[i], this);
this.parentNode.removeChild(this);
});
HTMLElement.prototype.__defineGetter__("canHaveChildren",function()
{
return !/^(area|base|basefont|col|frame|hr|img|br|input|isindex|link|meta|param)$/.test(this.tagName.toLowerCase());
});
}
//-->
</SCRIPT>
</head>
<body>
<div id="div1" style='background-color:tan'>aaa<b>bbb</b>ccc</div>
<SCRIPT LANGUAGE="JavaScript">
<!--
alert(document.getElementById("div1").outerHTML);
document.getElementById("div1").outerHTML = "<div style='font-weight: bold; background-color: yellow'>haha</div>";
alert(document.body.innerHTML);
//-->
</SCRIPT>
</body>
方法二:
function GetOuterHTML(element) {
element = element.cloneNode(true);
var outer = document.createElement("DIV");
outer.appendChild(element);
return outer.innerHTML;
}