1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
function getByClass(strClass, context) { context = context || document; if (document.getElementsByClassName) { return nodeListToArray(context.getElementsByClassName(strClass)); } var aryClass = strClass.match(/\S+/); var eles = nodeListToArray(context.getElementsByTagName('*')); for (var i = 0; i < aryClass; i++) { var reg = new RegExp('(^| +)' + aryClass[i] + '( +|$)'); for (var j = 0; j < eles.length; j++) { if (!reg.test(eles[i].className)) { eles.splice(j, 1); j--; } } } return eles; }
function hasClass(ele, strClass) { var reg = new RegExp("(^| +)" + strClass + "( +|$)"); return reg.test(ele.className) }
function addClass(ele, strClass) { var aryClass = strClass.replace(/(^\s+|\s+$)/g, "").split(/\s+/g); for (var i = 0; i < aryClass.length; i++) { var curClass = aryClass[i]; if (!hasClass(ele, curClass)) { ele.className += " " + curClass; } } }
function removeClass(ele, strClass) { var aryClass = strClass.replace(/^ +| +$/g, "").split(/ +/g); for (var i = 0; i < aryClass.length; i++) { var curClass = aryClass[i]; var reg = new RegExp("(^| +)" + curClass + "( +|$)", "g"); if (hasClass(ele, curClass)) { ele.className = ele.className.replace(reg, " "); } } }
|