163 AJAX Tab
程序员文章站
2022-07-02 16:46:43
复制代码 代码如下:// 163 ajax tab // update 2006.10.18&nb...
复制代码 代码如下:
// 163 ajax tab
// update 2006.10.18
// 增加鼠标延迟感应特性。
// update 2006.10.8
// a 标签 href 属性将保持原有html功能。增加urn属性为ajax load 路径。
// update 2006.10.11
// 修正ie5.0 undefined 未定义错误,增加脚本错误屏蔽
var browser = new object();
browser.ismozilla = (typeof document.implementation != 'undefined') && (typeof document.implementation.createdocument != 'undefined') && (typeof htmldocument!='undefined');
browser.isie = window.activexobject ? true : false;
browser.isfirefox = (navigator.useragent.tolowercase().indexof("firefox")!=-1);
browser.isopera = (navigator.useragent.tolowercase().indexof("opera")!=-1);
if (browser.isfirefox) { // entend event mod for firefox
extendeventobject();
}
function extendeventobject() {
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")
node = this.relatedtarget;
else if (this.type == "mouseout")
node = this.target;
if (!node) return;
while (node.nodetype != 1) node = node.parentnode;
return node;
});
event.prototype.__definegetter__("toelement", function () {
var node;
if (this.type == "mouseout")
node = this.relatedtarget;
else if (this.type == "mouseover")
node = this.target;
if (!node) return;
while (node.nodetype != 1) node = node.parentnode;
return node;
});
}
function ischild(cnode,pnode){
while(cnode!=null){
cnode=cnode.parentnode;
if(cnode==pnode) return true;
}
return false;
}
var ajccache=new object();
var waitinterval;
var tempref;
var mousedelaytime=150;//鼠标感应延迟300毫秒
function gettbprefixname(str,sta){
if(str.indexof("active")!=-1 || str.indexof("normal")!=-1) str=str.substr(6);
else if(str.indexof("over")!=-1) str=str.substr(4);
else str="";
return sta+str;
}
function startajaxtabs(){
for(var i=0;i<arguments.length;i++)
{
var ulobj=document.getelementbyid(arguments[i]);
ulist=ulobj.getelementsbytagname("li");
for(var j=0;j<ulist.length;j++)
{
var thelist=ulist[j];
if(thelist.parentnode.parentnode!=ulobj) continue;//只有第一层li有效 fixed 2006.9.29
var ulistlink=thelist.getelementsbytagname("a")[0];
var ulistlinkurl=ulistlink.getattribute("urn");
var ulistlinktarget=ulistlink.getattribute("rel");
thelist.setactive=function(bactive){
if(bactive){
this.status="active";
this.classname=gettbprefixname(this.classname,"active");
}else{
this.status="normal";
this.classname=gettbprefixname(this.classname,"normal");
}
}
thelist.loadtab=function(){
this.setactive(true);
this.parentnode.parentnode.activetab.setactive(false);
this.parentnode.parentnode.activetab=this;
var ulistlink=this.getelementsbytagname("a")[0];
loadajaxtab(ulistlink.getattribute("urn"),ulistlink.getattribute("rel"));
}
thelist.onmouseover=function(aevent){
var myevent = window.event ? window.event : aevent;
var fm=myevent.fromelement;
if(ischild(fm,this) || fm==this) return;//过滤子元素event
if(this.status=="active") return;
tempref=this;
cleartimeout(waitinterval);
waitinterval=window.settimeout("tempref.loadtab();",mousedelaytime);
}
thelist.onmouseout=function(aevent){
var myevent = window.event ? window.event : aevent;
var em=myevent.toelement;
if(ischild(em,this) || em==this) return; //过滤子元素event
if(this.status=="active") return;
cleartimeout(waitinterval);
}
if(ulistlinkurl.indexof("#default")!=-1){
thelist.setactive(true);
ulobj.activetab=thelist;
ajccache[ulistlinkurl]=getelement(ulistlinktarget).innerhtml;
}else{
thelist.setactive(false);
}
}
if(ulobj.activetab==null) ulobj.activetab=ulist[0];
}
}
function getxmlhttp()
{
var http_request;
if(window.xmlhttprequest) {
http_request = new xmlhttprequest();
if (http_request.overridemimetype) {
http_request.overridemimetype("text/xml");
}
}
else if (window.activexobject) {
try {
http_request = new activexobject("msxml2.xmlhttp");
} catch (e) {
try {
http_request = new activexobject("microsoft.xmlhttp");
} catch (e) {}
}
}
if (!http_request) {
window.alert("can't create xmlhttprequest object.");
return null;
}
return http_request;
}
function loadajaxtab(url,contentid){
var ocontent=getelement(contentid);
if(ajccache[url]==null) {
var xhttp=getxmlhttp();
xhttp.onreadystatechange=function(){
if(xhttp.readystate == 4 && (xhttp.status==200 || window.location.href.indexof("http")==-1))
{
ocontent.innerhtml=xhttp.responsetext;
ajccache[url]=ocontent.innerhtml;
}
}
xhttp.open("get",url,true);
xhttp.send(null);
}else{
ocontent.innerhtml=ajccache[url];
}
}
window.onerror=function(){return true}
具体使用方法,看用的