JS:setAttribute() getAttribute() 使用总结

作者 : admin 于 2008年10月07日, 23:53:38
2008
10-7

setAttribute() getAttribute() 两个函数分别是设置DOM节点属性设置和修改的函数,比如

var a=document.getElementById(’link’);

a.setAttribute(’class’) = ‘mylinkstyle’;

可以设置一个元素的属性

同样,我们可以自定义元素的属性,比如 <a href=”#” tips=”tipscontet” title=”testtitle”>test</a>

如此,给a标签增加一个新的属性,tips。而tips的值可以用getAttribute()获取。

简单的div页面拖动效果

作者 : admin 于 2008年08月07日, 23:33:26
2008
08-7

这里主要是一些元素坐标的计算,不过我写这个东西不很兼容firefox,正在查找原因。
index

兼容ie,firefox的页面事件监听函数

作者 : admin 于 2008年08月07日, 22:01:14
2008
08-7

listenerevent

  1. //for ff
  2. if(document.addEventListener)
  3. {
  4.     function __Eventadd ( element , eventType , handle )
  5. {
  6.     element.addEventListener( eventType , handle , false );
  7. }
  8. function __Eventremove ( element , eventType , handle )
  9. {
  10.     element.removeEventListener( eventType , handle , false );
  11. }
  12. }
  13. //for ie
  14. else if(document.attachEvent)
  15. {
  16.     function __Eventadd ( element , eventType , handle )
  17. {
  18.     element.attachEvent( 'on'+eventType , handle );
  19. }
  20. function __Eventremove ( element , eventType , handle )
  21. {
  22.     element.detachEvent( 'on'+eventType , handle );
  23. }
  24. }

实际上,真正完全兼容还要很复杂的一个过程,这里也是简化到了只是能用的程度。具体请查看《JavaScript权威指南》第五版 414页

几个JS函数,自家用,共享之

作者 : admin 于 2008年08月04日, 18:45:12
2008
08-4

检测浏览器类型,主要根据 userAgent关键字来确定

  1. function __navigator()
  2. {
  3.     this.value = false;   //返回值  ie 0 firefox 1 other 2
  4. this.useragent =  navigator['userAgent'];
  5. if(this.useragent.indexOf('MSIE')>0)
  6. {
  7.     this.value = 0;
  8. }
  9. else if(this.useragent.indexOf('Firefox')>0)
  10. {
  11.     this.value = 1;
  12. }
  13. else
  14. {
  15.     this.value = 2;
  16. }
  17. }

获得浏览器相关信息

  1. function __window()
  2. {
  3.     this.innerwidth = 0;   //document width
  4. this.innerheight = 0; //document height
  5.  
  6. this.outerwidth = 0;   //browser width
  7. this.outerwidth = 0;   //browser height
  8.  
  9. this.screenx = 0;     //browser position x
  10. this.screeny = 0;   //browser position y
  11.  
  12.     /*@cc_on
  13.   @if(@_jscript)
  14.  
  15.   this.innerwidth = document.body.clientWidth;
  16.   this.innerheight = document.body.clientHeight;
  17.  
  18.   this.outerwidth = document.documentElement.clientWidth;
  19.   this.outerheight = document.documentElement.clientHeight;
  20.  
  21.   this.screenx = window.screenLeft;
  22.   this.screeny = window.screenTop;
  23.  
  24.   @else*/
  25.  
  26.   this.innerwidth = window.innerWidth;
  27.   this.innerheight = window.innerHeight;
  28.  
  29.   this.outerwidth = window.outerWidth;
  30.   this.outerheight = window.outerHeight;
  31.  
  32.   this.screenx = window.screenX;
  33.   this.screeny = window.screenY;
  34. /*@end
  35.   @*/
  36. }

JavaScript跨浏览器兼容代码

作者 : admin 于 2008年08月03日, 22:58:07
2008
08-3

方法1:检测浏览器关键字

  1. function __navigator()
  2. {
  3.     this.value = false;   //返回值  ie 0 firefox 1 other 2
  4. this.useragent =  navigator['userAgent'];
  5. if(this.useragent.indexOf('MSIE')>0)
  6. {
  7.     this.value = 0;
  8. }
  9. else if(this.useragent.indexOf('Firefox')>0)
  10. {
  11.     this.value = 1;
  12. }
  13. else
  14. {
  15.     this.value = 2;
  16. }
  17. }

方法2:

  1. if(document.all)
  2.   ie
  3. else
  4.   other

方法3:

  1. <!--[if IE]>
  2.     js for ie
  3. <![endif]-->
  4. <!--[if !IE]>
  5.     js for !ie
  6. <![endif]-->

方法4:

  1. /*@cc_on
  2.   @if(@_jscript)
  3.   is for ie
  4.   @else*/
  5.   js for !ie
  6. /*@end
  7.   @*/

一个操作Cookie的小程序

作者 : admin 于 2008年07月30日, 22:57:24
2008
07-30

simplecookie

  1. <br />
  2. /*<br />
  3.     一个简单的cookie操作函数,自家用<br />
  4. */<br />
  5. function SetCookie()<br />
  6. {<br />
  7.     var name = arguments[0];<br />
  8. var value = arguments[1];<br />
  9. //初始化默认存储时间<br />
  10. var time = 3600;<br />
  11. if(arguments[2]!=undefined)<br />
  12. {<br />
  13.     time = arguments[2]<br />
  14. }<br />
  15. if(arguments[0]==undefined||arguments[0]==undefined)<br />
  16. {<br />
  17.     alert("SetCookie Has ERROR Agruments");<br />
  18. }<br />
  19. document.cookie = name+"="+escape(value)+" ; expires="+GetTime(time);<br />
  20. }<br />
  21. function GetCookie( name )<br />
  22. {<br />
  23.     var arr = document.cookie.match(new RegExp("(^|)"+name+"=([^;]*)(;|$)"));<br />
  24. if(arr!=null)<br />
  25. {<br />
  26.     return unescape(arr[2]);<br />
  27. }<br />
  28. else<br />
  29. {<br />
  30.     return null;<br />
  31. }<br />
  32. }<br />
  33. function DelCookie( name )<br />
  34. {<br />
  35.     var value = GetCookie( name );<br />
  36. if(value!=null)<br />
  37. {<br />
  38.     document.cookie = name+"="+escape(value)+" ; expires="+GetTime( 0 );<br />
  39. }<br />
  40. }<br />
  41. function GetTime( time )<br />
  42. {<br />
  43.     var exp = new Date();<br />
  44. exp.setTime( exp.getTime()+time*1000 );<br />
  45. return exp.toGMTString();<br />
  46. }<br />