四
01
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 | /* * Get the class string from the object * @param object o the DOM object * @return string the class string of the object */ //var getClass=function(o){ var getClassName=function(o){ return o.className; } /* * Set the class string from the object * @param string cls the class string to be set * @param object o the DOM object where * the class string to be set to */ //var setClass=function(o){ var setClassName=function(cls,o){ o.className=cls; } /* * Parse the classes list to array by split(" ") * @param string cl a string of class or a list of classes * @return array with the element of class name */ var getClassArray=function(cl){ if(cl && typeof(cl)=="string") return cl.split(" "); else return cl; } /* * If the class is listed in the classes list ,return true * @param string cl a single class name * @param string cls class name list * @return true if the class is listed * in the classes list.else return false. */ var matchClass=function(cl,cls){ if(!cls) return false; cls= getClassArray(cls); for(var c in cls) if(cl==cls[c]) return true; return cl==cls; } /* * remove one of the classes from the object * @param string cl a single class name * @param string cls class name list */ var removeClass=function(cl,o){ var cls=getClassArray(getClassName(o)); var _cls=""; for(var i in cls) if (cl!=cls[i]) _cls+=" "+cls[i]; setClassName(_cls,o); } /* * add a class to the object * @param string cl a single class name * @param object o the DOM object * where the class string to be ADD to */ var addClass=function(cl,o){ removeClass(cl,o); setClassName(getClassName(o)+" "+cl,o); } |
上面几个函数,相信很多人早就写过不少,在这里我仅为总结一下经验,也希望有点价值。
通常,在xhtml中标签改变class的值,只要
obj.className=stringValue
这是对付简单的情况。当一个元素中已经有多个class时,一个简单的obj.className=stringValue就无能为力了。 阅读全文(Read the rest of this entry) »
Popularity: 52% [?]
文章字体大小:

tags: