Skip to content

Instantly share code, notes, and snippets.

@solodxg
Created December 26, 2012 11:34
Show Gist options
  • Save solodxg/4379805 to your computer and use it in GitHub Desktop.
Save solodxg/4379805 to your computer and use it in GitHub Desktop.
js:tab切换
Function.prototype.getCallback = function(){
var _this = this, args = arguments;
return function(e) {
return _this.apply(this || window, args);
};
}
function $(i) {
return document.getElementById(i)
}
function getTag(p, e) {
return p.getElementsByTagName(e)
}
function addEventHandler(target, type, func) {
if(target.addEventListener) {
target.addEventListener(type, func, false);
} else if(target.attachEvent) {
target.attachEvent("on" + type, func);
} else {
target["on" + type] = func;
}
}
function tabSet(t, c) {
var tab = $(t);
var con = $(c);
var tlist = getTag(tab, 'li');
var clist = getTag(con, 'div');
var tlist = getTag(tab, 'li');
for(var i = 0; i < tlist.length; i++) {
addEventHandler(tlist[i], "click", changeTab.getCallback(tlist,clist));
}
}
function changeTab(tl,cl) {
for(i = 0; i < tl.length; i++) {
tl[i].className = "";
cl[i].style.display = "none";
if(tl[i] == this) {
tl[i].className = "current";
cl[i].style.display = "block";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment