User:Locke/common.js: Difference between revisions
Jump to navigation
Jump to search
Want an adless experience? Log in or Create an account.
No edit summary |
No edit summary |
||
Line 88: | Line 88: | ||
/* This assumes that every tabcontent div is paired with one tab div. */ | /* This assumes that every tabcontent div is paired with one tab div. */ | ||
var destinations = getElementsByClass("tab"); | var destinations = getElementsByClass("tab"); | ||
alert(destinations.length); | |||
var tabcontents = getElementsByClass("tabcontent"); | var tabcontents = getElementsByClass("tabcontent"); | ||
alert(tabcontents.length); | |||
for( var i = 0; i < tabcontents.length; ++i ) { | for( var i = 0; i < tabcontents.length; ++i ) { | ||
var tabs = getChildrenByTagName(tabcontents[i], "ul"); | var tabs = getChildrenByTagName(tabcontents[i], "ul"); | ||
alert(tabs.length); | |||
for( var j = 0; j < tabs.length; ++j ) { | for( var j = 0; j < tabs.length; ++j ) { | ||
/* Move from tabcontents ul to tab ul */ | /* Move from tabcontents ul to tab ul */ |
Revision as of 19:32, January 1, 2013
/* Developed by Robert Nyman, http://www.robertnyman.com Code/licensing: http://code.google.com/p/getelementsbyclassname/ */ var getElementsByClassName = function (className, tag, elm){ if (document.getElementsByClassName) { getElementsByClassName = function (className, tag, elm) { elm = elm || document; var elements = elm.getElementsByClassName(className), nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null, returnElements = [], current; for(var i=0, il=elements.length; i<il; i+=1){ current = elements[i]; if(!nodeName || nodeName.test(current.nodeName)) { returnElements.push(current); } } return returnElements; }; } else if (document.evaluate) { getElementsByClassName = function (className, tag, elm) { tag = tag || "*"; elm = elm || document; var classes = className.split(" "), classesToCheck = "", xhtmlNamespace = "http://www.w3.org/1999/xhtml", namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null, returnElements = [], elements, node; for(var j=0, jl=classes.length; j<jl; j+=1){ classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]"; } try { elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null); } catch (e) { elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null); } while ((node = elements.iterateNext())) { returnElements.push(node); } return returnElements; }; } else { getElementsByClassName = function (className, tag, elm) { tag = tag || "*"; elm = elm || document; var classes = className.split(" "), classesToCheck = [], elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag), current, returnElements = [], match; for(var k=0, kl=classes.length; k<kl; k+=1){ classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)")); } for(var l=0, ll=elements.length; l<ll; l+=1){ current = elements[l]; match = false; for(var m=0, ml=classesToCheck.length; m<ml; m+=1){ match = classesToCheck[m].test(current.className); if (!match) { break; } } if (match) { returnElements.push(current); } } return returnElements; }; } return getElementsByClassName(className, tag, elm); }; function getChildrenByTagName(parent, name) { var nodeList = []; for (var child = parent.firstChild; child != null; child = child.nextSibling) { if (child.nodeType == 1 && name == child.nodeName) { nodeList.push(child); } } return nodeList; } /* This assumes that every tabcontent div is paired with one tab div. */ var destinations = getElementsByClass("tab"); alert(destinations.length); var tabcontents = getElementsByClass("tabcontent"); alert(tabcontents.length); for( var i = 0; i < tabcontents.length; ++i ) { var tabs = getChildrenByTagName(tabcontents[i], "ul"); alert(tabs.length); for( var j = 0; j < tabs.length; ++j ) { /* Move from tabcontents ul to tab ul */ destinations[i].firstChild.appendChild(tabs[j].firstChild); /* Remove the now empty tabcontents ul */ tabcontents[i].removeChild(tabs[j]); } }