JS整理,getCookie, getElementsByClassName , 添加删除事件
Posted on 2009-03-13 13:37 Heclei 阅读(530) 评论(0) 编辑 收藏 举报Cookie:
Cookie
1function getCookie(c_name)
2{
3 if (document.cookie.length>0)
4 {
5 c_start=document.cookie.indexOf(c_name + "=");
6 if (c_start!=-1)
7 {
8 c_start=c_start + c_name.length+1;
9 c_end=document.cookie.indexOf(";",c_start);
10 if (c_end==-1)
11 c_end=document.cookie.length;
12 return decodeURIComponent(document.cookie.substring(c_start,c_end));
13 }
14 }
15 return null;
16}
17
18function setCookie(c_name,value,expiredays)
19{
20 var exdate=new Date();
21 exdate.setDate(exdate.getDate()+expiredays);
22 document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate);
23}
1function getCookie(c_name)
2{
3 if (document.cookie.length>0)
4 {
5 c_start=document.cookie.indexOf(c_name + "=");
6 if (c_start!=-1)
7 {
8 c_start=c_start + c_name.length+1;
9 c_end=document.cookie.indexOf(";",c_start);
10 if (c_end==-1)
11 c_end=document.cookie.length;
12 return decodeURIComponent(document.cookie.substring(c_start,c_end));
13 }
14 }
15 return null;
16}
17
18function setCookie(c_name,value,expiredays)
19{
20 var exdate=new Date();
21 exdate.setDate(exdate.getDate()+expiredays);
22 document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate);
23}
getElementsByClassName:
getElementsByClassName
1function getElementsByClassName(searchClass, domNode, tagName) {
2 alert(searchClass);
3 if (domNode == null) domNode = document;
4 if (tagName == null) tagName = '*';
5 var el = new Array();
6 var tags = domNode.getElementsByTagName(tagName);
7 var tcl = " "+searchClass+" ";
8 for(i=0,j=0; i<tags.length; i++) {
9 var test = " " + tags[i].className + " ";
10 if (test.indexOf(tcl) != -1)
11 el[j++] = tags[i];
12 }
13
14 alert(el);
15 return el;
16}
1function getElementsByClassName(searchClass, domNode, tagName) {
2 alert(searchClass);
3 if (domNode == null) domNode = document;
4 if (tagName == null) tagName = '*';
5 var el = new Array();
6 var tags = domNode.getElementsByTagName(tagName);
7 var tcl = " "+searchClass+" ";
8 for(i=0,j=0; i<tags.length; i++) {
9 var test = " " + tags[i].className + " ";
10 if (test.indexOf(tcl) != -1)
11 el[j++] = tags[i];
12 }
13
14 alert(el);
15 return el;
16}
Event
Event
1var EventUtil=new Object;
2 //oTarget:目标;sEventType:事件名称;funName:事件触发的函数名;
3 EventUtil.addEvent=function(oTarget,sEventType,funName){
4 if(oTarget.addEventListener){//for DOM;
5 oTarget.addEventListener(sEventType,funName, false);
6 }else if(oTarget.attachEvent){
7 oTarget.attachEvent("on"+sEventType,funName);
8 }else{
9 oTarget["on"+sEventType]=funName;
10 }
11 };
12
13 EventUtil.removeEvent=function(oTarget,sEventType,funName){
14 if(oTarget.removeEventListener){//for DOM;
15 oTarget.removeEventListener(sEventType,funName, false);
16 }else if(oTarget.detachEvent){
17 oTarget.detachEvent("on"+sEventType,funName);
18 }else{
19 oTarget["on"+sEventType]=null;
20 }
21 };
1var EventUtil=new Object;
2 //oTarget:目标;sEventType:事件名称;funName:事件触发的函数名;
3 EventUtil.addEvent=function(oTarget,sEventType,funName){
4 if(oTarget.addEventListener){//for DOM;
5 oTarget.addEventListener(sEventType,funName, false);
6 }else if(oTarget.attachEvent){
7 oTarget.attachEvent("on"+sEventType,funName);
8 }else{
9 oTarget["on"+sEventType]=funName;
10 }
11 };
12
13 EventUtil.removeEvent=function(oTarget,sEventType,funName){
14 if(oTarget.removeEventListener){//for DOM;
15 oTarget.removeEventListener(sEventType,funName, false);
16 }else if(oTarget.detachEvent){
17 oTarget.detachEvent("on"+sEventType,funName);
18 }else{
19 oTarget["on"+sEventType]=null;
20 }
21 };
比较实用的,不需要很多无用的代码.