Created
December 26, 2012 11:34
-
-
Save solodxg/4379805 to your computer and use it in GitHub Desktop.
js:tab切换
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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