(推荐一个超好的JS函数库)S.Sams Lifexperience ScriptClassLib
(推荐一个超好的JS函数库)S.Sams Lifexperience ScriptClassLib
发布时间:2016-12-30 来源:查字典编辑
摘要:1/**//*------------------------------------------------------------2*S...

1/**//*------------------------------------------------------------

2*S.SamsLifexperience

3*CopyRight(C)2003-2007S.SamsLifexperienceScriptClassLib

4*MSNLive:S.Sams#msn.com

5*Http://blog.8see.net/

6*UPdateby:2007-01-19转载传播请保留版权

7*-----------------------------------------------------------*/

8

9

10/**//*$获取指定对象

11@element对象名

12可以使用对象名集合,返回值为对象的集合

13如果您使用了Prototype类库,请把该函数注释掉

14Sams_object.Get()中同样实现该函数的所有功能

15*/

16function$(element){

17if(arguments.length>1){

18for(vari=0,elements=[],length=arguments.length;i<length;i++)

19elements.push($(arguments[i]));

20returnelements;

21}

22if(typeofelement=='string')

23element=document.getElementById(element);

24returnelement;

25}

26

27///浏览器相关操作

28varSams_browse={

29/**//*检测浏览信息*/

30checkBrowser:function()

31{

32this.ver=navigator.appVersion

33this.dom=document.getElementById?1:0

34this.ie6=(this.ver.indexOf("MSIE6")>-1&&this.dom)?1:0;

35this.ie5=(this.ver.indexOf("MSIE5")>-1&&this.dom)?1:0;

36this.ie4=(document.all&&!this.dom)?1:0;

37this.ns5=(this.dom&&parseInt(this.ver)>=5)?1:0;

38this.ns4=(document.layers&&!this.dom)?1:0;

39this.mac=(this.ver.indexOf('Mac')>-1)?1:0;

40this.ope=(navigator.userAgent.indexOf('Opera')>-1);

41this.ie=(this.ie6||this.ie5||this.ie4)

42this.ns=(this.ns4||this.ns5)

43this.bw=(this.ie6||this.ie5||this.ie4||this.ns5||this.ns4||this.mac||this.ope)

44this.nbw=(!this.bw)

45returnthis;

46},

47

48/**//*设为首页

49@url要设为首页的地址

50*/

51SetDefault:function()

52{

53this.style.behavior='url(#default#homepage)';

54this.setHomePage(this.GetUrl());

55returnfalse;

56},

57

58/**//*复制指定URL地址

59@Msg要写入剪贴板的字符集

60*/

61SetCopy:function(Msg){

62if(navigator.userAgent.toLowerCase().indexOf('ie')>-1){

63clipboardData.setData('Text',Msg);

64alert("网址“"+Msg+"”n已经复制到您的剪贴板中n您可以使用Ctrl+V快捷键粘贴到需要的地方");

65}

66else

67{

68prompt("请复制网站地址:",Msg);

69}

70},

71

72/**//*加入收藏

73@site站点名称

74@url地址

75*/

76AddBookmark:function(site,url){

77if(navigator.userAgent.toLowerCase().indexOf('ie')>-1){

78window.external.addFavorite(url,site)

79}elseif(navigator.userAgent.toLowerCase().indexOf('opera')>-1){

80alert("请使用Ctrl+T将本页加入收藏夹");

81}else{

82alert("请使用Ctrl+D将本页加入收藏夹");

83}

84},

85

86/**//*打开Url指定宽度和高度的窗口*/

87OpenWindows:function(url,width,height)

88{

89window.open(url,'newwin','width='+width+',height='+height);

90returnfalse;

91},

92

93/**//*禁止浏览器的Javascript错误提示*/

94CloseError:function(){

95window.onerror=function(){returntrue;};

96},

97

98/**//*获取浏览器URL*/

99GetUrl:function(){

100returnlocation.href;

101},

102

103/**//*获取URL参数*/

104GetUrlParam:function(){

105returnlocation.search;

106},

107

108/**//*获取页面来源*/

109GetFrom:function(){

110returndocument.referrer;

111},

112

113/**//*获取指定的URL参数值

114@name参数名

115*/

116Request:function(name){

117varGetUrl=this.GetUrl();

118varPlist=newArray();

119if(GetUrl.indexOf('?')>0)

120{

121Plist=GetUrl.split('?')[1].split('&');

122}

123elseif(GetUrl.indexOf('#')>0)

124{

125Plist=GetUrl.split('#')[1].split('&');

126}

127if(GetUrl.length>0)

128{

129for(vari=0;i<Plist.length;i++)

130{

131varGetValue=Plist[i].split('=');

132if(GetValue[0].toUpperCase()==name.toUpperCase())

133{

134returnGetValue[1];

135break;

136}

137}

138return;

139}

140},

141

142/**//*直接将HTML写到新窗口

143@title标题

144@msg内容

145*/

146Popmsg:functionPopIt(title,msg)

147{

148varpopup=window.open('','popDialog','height=500,width=400,scrollbars=yes');

149popup.document.write('<html><title>'+title+'</title><style>body{margin:10px;font:13pxArial;}span{text-line:20px;}</style><body><spanstyle='font:14pxarial;'>'+msg+'</span></body></html>');

150popup.document.close();

151}

152};

153

154

155///对象操作

156varSams_object={

157

158/**//*创建一个DIV对象

159@ID要创建的对象ID

160@ClassName创建对象的Class

161@SetValue设置该对象值

162@ToDiv将对象追加到指定的对象,如指定的对象不存在,则追加在Body的后面

163返回创建后的对象

164*/

165CreateDiv:function(ID,ClassName,SetValue,ToDiv){

166varcreatediv=document.createElement('div');

167if(ID!=null)creatediv.id=ID;

168creatediv.style.position='absolute';

169if(ClassName!=null)creatediv.className=ClassName;

170if(this.Get(ToDiv))

171{

172this.Get(ToDiv).appendChild(creatediv);

173}

174else

175{

176document.getElementsByTagName('body')[0].appendChild(creatediv);

177}

178this.SetValue(ID,SetValue);

179returnthis.Get(ID);

180},

181

182/**//*删除指定DIV对象

183@objid要删除的对象ID

184返回Bool操作结果

185*/

186DeleteDiv:function(objid)

187{

188try

189{

190if(this.Get(objid))

191{

192varGetParent=this.Get(objid).parentNode;

193GetParent.removeChild(this.Get(objid));

194returntrue;

195}

196else

197{

198returnfalse;

199}

200}

201catch(e)

202{

203returnfalse;

204}

205},

206

207/**//*获取浏览器对象

208@id要获取的对象ID

209可以使用对象名集合,返回值为对象的集合

210*/

211Get:function(objid){

212if(arguments.length>1){

213for(vari=0,objids=[],length=arguments.length;i<length;i++)

214objids.push(this.Get(arguments[i]));

215returnobjids;

216}

217if(typeofobjid=='string')

218{

219if(document.getElementById){

220objid=document.getElementById(objid);

221}elseif(document.all){

222objid=document.all[objid];

223}elseif(document.layers){

224objid=document.layers[objid];

225}

226}

227returnobjid;

228},

229

230/**//*获取对象的值

231@objid对象ID

232*/

233GetValue:function(objid){

234if(typeofobjid=='string')

235{

236vargetTagName=this.Get(objid).tagName.toLowerCase();

237if(getTagName=='input'||getTagName=='textarea'||getTagName=='select')

238{

239returnthis.Get(objid).value;

240}

241elseif(getTagName=='div'||getTagName=='span')

242{

243returnthis.Get(objid).innerText;

244}

245}

246elseif(typeofobjid=='object')

247{

248returnobjid.value;

249}

250},

251

252/**//*设置指定对象的值,实现可以直接赋值或清除操作

253@objid对象ID

254@inserValue传入值(可选项Null:清除该ID的值,则直接赋值)

255*/

256SetValue:function(objid,inserValue){

257vargetTagName=this.Get(objid).tagName.toLowerCase();

258if(inserValue==null)inserValue='';

259if(getTagName=='input'||getTagName=='textarea')

260{

261this.Get(objid).value=inserValue;

262}

263elseif(getTagName=='div'||getTagName=='sapn')

264{

265

266this.Get(objid).innerText=inserValue;

267}

268},

269

270/**//*拷贝对象值到剪贴板

271@str对象值

272*/

273CopyCode:function(str){

274varrng=document.body.createTextRange();

275rng.moveToElementText(str);

276rng.scrollIntoView();

277rng.select();

278rng.execCommand("Copy");

279rng.collapse(false);

280},

281

282/**//*显示隐藏一个对象

283@Objid对象ID

284@isshow具体操作,指定Obj为False:none或者True:block(可选)

285*/

286ShowHidd:function(objid,isshow){

287if(isshow!=null)

288{

289if(isshow)

290{

291this.Get(objid).style.display='block';

292}

293else

294{

295this.Get(objid).style.display='none';

296}

297}

298else

299{

300if(this.Get(objid).style.display=='none')

301{

302this.Get(objid).style.display='block';

303}

304else

305{

306this.Get(objid).style.display='none';

307}

308}

309},

310

311/**//*当前对象是否可见

312@objid对象ID

313*/

314IsVisible:function(objid){

315if(this.Get(objid))

316{

317try

318{

319if(this.Get(objid).style.display=='none')

320{

321returnfalse

322}

323if(this.Get(objid).style.visibility=='hidden')

324{

325returnfalse;

326}

327returntrue;

328}

329catch(e)

330{

331returnfalse;

332}

333}

334else

335{

336returnfalse;

337}

338}

339};

340

341///字符处理

342varSams_string={

343/**//*取左边的指定长度的值

344@str要处理的字符集

345@n长度

346*/

347Left:function(str,n)

348{

349if(str.length>0)

350{

351if(n>str.length)n=str.length;

352returnstr.substr(0,n)

353}

354else

355{

356return;

357}

358},

359

360/**//*取右边的指定长度的值

361@str要处理的字符集

362@n长度

363*/

364Right:function(str,n)

365{

366if(str.length>0)

367{

368if(n>=str.length)returnstr;

369returnstr.substr(str.length-n,n);

370}

371else

372{

373return;

374}

375},

376

377/**//*Trim:清除两边空格

378@str要处理的字符集

379*/

380Trim:function(str)

381{

382if(typeofstr=='string')returnstr.replace(/(^s*)|(s*$)/g,'');

383},

384

385/**//*LTrim:清除左边的空格

386@str要处理的字符集

387*/

388Ltrim:function(str)

389{

390if(typeofstr=='string')returnstr.replace(/(^s*)/g,'');

391},

392

393/**//*RTrim:清除右边的空格

394@str要处理的字符集

395*/

396Rtrim:function(str)

397{

398if(typeofstr=='string')returnstr.replace(/(s*$)/g,'');

399},

400

401/**//*清除前后的非字符

402@str要处理的字符集

403*/

404strip:function(str){

405if(typeofstr=='string')returnstr.replace(/^s+/,'').replace(/(^s*)|(s*$)/g,'');

406},

407

408/**//*过滤字符里面的HTML标签

409@str要处理的字符集

410*/

411stripTags:function(str){

412if(typeofstr=='string')returnstr.replace(/</?[^>]+>/gi,'').replace(/(^s*)|(s*$)/g,'');

413}

414};

415

416///时间相关操作

417varSams_time={

418/**//*获取当天日期yyyy-MM-dd*/

419GetDateNow:function(){

420vard,y,m,dd;

421d=newDate();

422y=d.getYear();

423m=d.getMonth()+1;

424dd=d.getDate();

425returny+"-"+m+"-"+dd;

426},

427

428/**//*获取指定日期后的特定天数的日期值

429@toDate当前指定的日期

430@N要添加的日期数

431*/

432AddDays:function(toDate,N){

433varaDate=this._cvtISOToDate(toDate);

434if(!aDate)return"";

435varmillis=86400000*N;

436aDate=newDate(aDate.getTime()+millis);

437returnthis._fmtDateISO(aDate);

438},

439_fmtDateISO:function(aDate){

440with(aDate){

441varmm=getMonth()+1;

442if(mm<10){mm='0'+mm;}

443vardd=getDate();

444if(dd<10){dd='0'+dd;}

445return(getFullYear()+'-'+mm+'-'+dd);

446}

447},

448_cvtISOToDate:function(isoDate){

449varatomDate=isoDate.split('-');

450varaDate=newDate(parseInt(atomDate[0],10),parseInt(atomDate[1],10)-1,parseInt(atomDate[2],10),6,0,0);

451returnaDate;

452}

453};

454

455///图像相关操作

456varSams_media={

457/**//*为单一图像添加鼠标中键放大缩小功能,批量可以直接用ResizeImage(指定添加该功能的图片大小:Int)即可(该功能只适用于IE)

458objid对象ID

459*/

460ZoomFun:function(objid){

461Sams_object.Get(objid).onmousewheel=function(){returnSams_media.imagecontrol(this);}

462},

463

464/**//*重置图片尺寸同时添加放大功能(该功能只适用于IE)

465@IntSize指定图像的大小

466如果适合图像大小就添加放大缩小功能

467*/

468ResizeImage:function(IntSize){

469varimgsinlog=document.getElementsByTagName('img');

470for(j=0;j<imgsinlog.length;j++){

471if(imgsinlog[j].width>=IntSize){

472imgsinlog[j].width=IntSize;

473imgsinlog[j].style.cursor='pointer';

474imgsinlog[j].onclick=function(){window.open(this.src);}

475if(navigator.userAgent.toLowerCase().indexOf('ie')>-1){

476imgsinlog[j].title='您可以用鼠标中键或者使用Ctrl+鼠标滚轮缩放图片,点击图片可在新窗口打开';

477imgsinlog[j].onmousewheel=function(){returnSams_media.imagecontrol(this);};

478}

479else

480{

481imgsinlog[j].title='点击图片可在新窗口打开';

482}

483}

484}

485},

486imagecontrol:function(obj){

487varzoom=parseInt(obj.style.zoom,10)||100;zoom+=event.wheelDelta/12;

488if(zoom>0)obj.style.zoom=zoom+'%';

489returnfalse;

490},

491

492/**//*如果图像出现下载不了等异常,显示的错误提示图片

493@errimgpath显示错误提示的图像路径

494*/

495ImagesError:function(errimgpath){

496varimglist=document.getElementsByTagName('img');

497for(j=0;j<imglist.length;j++){

498imglist[j].onerror=function(){

499this.src=errimgpath;

500}

501}

502},

503

504/**//*显示媒体

505@mFile文件路径

506@mFileType文件类型(可为空,如为Flash,要指定为swf类型)

507@ObjID对象ID

508@mWidth显示的对象宽度

509@mHeight显示对象的高度

510注:可以指定对象的ID,如果ID不存在,会自动创建,追加在Body后面

511*/

512ShowMedia:function(mFile,mFileType,ObjID,mWidth,mHeight){

513varmediaStr;

514switch(mFileType){

515case"swf":

516mediaStr="<objectcodeBase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0'classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'width='"+mWidth+"'height='"+mHeight+"'><paramname='movie'value='"+mFile+"'><paramname='quality'value='high'><paramname='AllowScriptAccess'value='never'><embedsrc='"+mFile+"'quality='high'pluginspage='http://www.macromedia.com/go/getflashplayer'type='application/x-shockwave-flash'width='"+mWidth+"'height='"+mHeight+"'></embed></OBJECT>";

517break;

518default:

519mediaStr="<objectwidth='"+mWidth+"'height='"+mHeight+"'classid='CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6'><paramname='url'value='"+mFile+"'/><embedwidth='"+mWidth+"'height='"+mHeight+"'type='application/x-mplayer2'src='"+mFile+"'></embed></object>";

520}

521

522varmediaDiv=Sams_object.Get(ObjID);

523

524if(mediaDiv){

525mediaDiv.innerHTML=mediaStr;

526}

527else

528{

529mediaDiv=document.createElement("div");

530mediaDiv.id=ObjID;

531mediaDiv.innerHTML=mediaStr;

532document.getElementsByTagName('body')[0].appendChild(mediaDiv);

533}

534returnfalse;

535}

536};

537

538///样式相关操作

539varSams_style={

540/**//*改变字体大小

541@objid对象ID

542@size字号

543*/

544doZoom:function(objid,size){

545Sams_object.Get(objid).style.fontSize=size+'px';

546},

547

548/**//*改变指定对象样式

549@objid对象ID

550@className要更改的ClassName

551*/

552ClassName:function(objid,className){

553Sams_object.Get(objid).className=className;

554},

555

556/**//*对象定位

557@obj要定位的对象

558返回X.Y结果的数组对象

559*/

560GotoXY:function(obj){

561vart=obj.offsetTop;

562varl=obj.offsetLeft;

563while(obj=obj.offsetParent){

564t+=obj.offsetTop;

565l+=obj.offsetLeft;

566}

567returnArray(t,l);

568}

569};

570

571///科学计算

572varSams_account={

573/**//*逢1进10计算

574@数值

575*/

576GetTen:function(i)

577{

578varitems_One,Get_One;

579if(i.length>1&&(/^d+$/.test(i)))

580{

581items_One=i.substr(0,i.length-1);

582Get_One=i.substr(i.length-1,1);

583if(parseInt(Get_One)>0)

584{

585items_One=parseInt(items_One)+1;

586items_One=items_One+'0';

587}

588else

589{

590items_One=items_One+'0';

591}

592}

593else

594{

595items_One=i;

596}

597returnitems_One;

598}

599};

600

601///数据验证(所有数值返回值均为Bool型)

602varSams_validate={

603/**//*是否是数字型数据

604@str字符集

605*/

606IsNumber:function(str){

607if(/^d+$/.test(str)){returntrue;}else{returnfalse;}

608},

609

610/**//*是否是数字型数据

611@objid对象ID

612*/

613IsNumberObj:function(objid){

614returnthis.IsNumber(Sams_object.GetValue(objid));

615},

616

617/**//*是否是自然数型数据

618@str字符集

619*/

620IsInt:function(str){

621if(/^(+|-)?d+$/.test(str)){returntrue;}else{returnfalse;}

622},

623

624/**//*是否是自然数型数据

625@objid对象ID

626*/

627IsIntObj:function(objid){

628returnthis.IsInt(Sams_object.GetValue(objid));

629},

630

631/**//*是否是中文字符

632@str字符集

633*/

634IsChinese:function(str)

635{

636if(/^[u4e00-u9fa5]+$/.test(str)){returntrue;}else{returnfalse;}

637},

638

639/**//*是否是中文字符

640@objid对象ID

641*/

642IsChineseObj:function(objid)

643{

644returnthis.IsChinese(Sams_object.GetValue(objid));

645},

646

647/**//*是否为英文字母

648@str字符集

649*/

650IsLower:function(str)

651{

652if(/^[A-Za-z]+$/.test(str)){returntrue}else{returnfalse;}

653},

654

655/**//*是否为英文字母

656@objid对象ID

657*/

658IsLowerObj:function(objid)

659{

660returnthis.IsLower(Sams_object.GetValue(objid));

661},

662

663/**//*是否为正确的网址

664@str字符集

665*/

666IsUrl:function(str)

667{

668varmyReg=/^((http:[/][/])?w+([.]w+|[/]w*)*)?$/;

669if(myReg.test(str)){returntrue;}else{returnfalse;}

670},

671

672/**//*是否为正确的网址

673@objid对象ID

674*/

675IsUrlObj:function(objid)

676{

677returnthis.IsUrl(Sams_object.GetValue(objid));

678},

679

680/**//*是否为正确的Email形式

681@str字符集

682*/

683IsEmail:function(str)

684{

685varmyReg=/^([-_A-Za-z0-9.]+)@([_A-Za-z0-9]+.)+[A-Za-z0-9]{2,3}$/;

686if(myReg.test(str)){returntrue;}else{returnfalse;}

687},

688

689

690/**//*是否为正确的Email形式

691@objid对象ID

692*/

693IsEmailObj:function(objid)

694{

695returnthis.IsEmail(Sams_object.GetValue(objid));

696},

697

698/**//*是否为正确的手机号码

699@str字符集

700*/

701IsMobile:function(str)

702{

703varregu=/(^[1][3][0-9]{9}$)|(^0[1][3][0-9]{9}$)/;

704varre=newRegExp(regu);

705if(re.test(str)){returntrue;}else{returnfalse;}

706},

707

708/**//*是否为正确的手机号码

709@objid对象ID

710*/

711IsMobileObj:function(objid)

712{

713returnthis.IsMobile(Sams_object.GetValue(objid));

714}

715};

716

717/**//*

718实现Ajax功能

719Sams_ajax.SendRequest('GET',url,null,recall,"addtohome");

720Sams_ajax.SendRequest('GET',url,null,null);

721obj.responseText;

722*/

723varSams_ajax={

724_objPool:[],

725_getInstance:function(){

726for(vari=0;i<this._objPool.length;i++){

727if(this._objPool[i].readyState==0||this._objPool[i].readyState==4){

728returnthis._objPool[i];

729}

730}

731this._objPool[this._objPool.length]=this._createObj();

732returnthis._objPool[this._objPool.length-1];

733},

734_createObj:function(){

735if(window.XMLHttpRequest){

736varobjXMLHttp=newXMLHttpRequest();

737}

738else{

739varMSXML=['MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP'];

740for(varn=0;n<MSXML.length;n++){

741try{

742varobjXMLHttp=newActiveXObject(MSXML[n]);

743break;

744}

745catch(e){

746}

747}

748}

749if(objXMLHttp.readyState==null){

750objXMLHttp.readyState=0;

751objXMLHttp.addEventListener("load",function(){

752objXMLHttp.readyState=4;

753if(typeofobjXMLHttp.onreadystatechange=="function"){

754objXMLHttp.onreadystatechange();

755}

756},false);

757}

758returnobjXMLHttp;

759},

760

761///开始发送请求

762SendRequest:function(method,url,data,callback,funparam,funparam2){

763varobjXMLHttp=this._getInstance();

764with(objXMLHttp){

765try{

766if(url.indexOf("?")>0){

767url+="&randnum="+Math.random();

768}

769else{

770url+="?randnum="+Math.random();

771}

772open(method,url,true);

773setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=UTF-8');

774send(data);

775onreadystatechange=function(){

776if(objXMLHttp.readyState==4&&(objXMLHttp.status==200||objXMLHttp.status==304))

777{

778callback(objXMLHttp,funparam,funparam2);

779}else{

780callback(null,funparam,funparam2);

781}

782}

783}

784catch(e){

785alert(e);

786}

787}

788}

789};

790

791///Cookies操作

792varSams_cookies={

793/**//*cookies设置函数

794@nameCookies名称

795@value值

796*/

797setCookie:function(name,value)

798{

799try

800{

801varargv=setCookie.arguments;

802varargc=setCookie.arguments.length;

803varexpires=(argc>2)?argv[2]:null;

804if(expires!=null)

805{

806varLargeExpDate=newDate();

807LargeExpDate.setTime(LargeExpDate.getTime()+(expires*1000*3600*24));

808}

809document.cookie=name+"="+escape(value)+((expires==null)?"":(";expires="+LargeExpDate.toGMTString()));

810returntrue;

811}

812catch(e)

813{

814returnfalse;

815}

816},

817

818/**//*cookies读取函数

819@NameCookies名称

820返回值Cookies值

821*/

822getCookie:function(Name)

823{

824varsearch=Name+"="

825if(document.cookie.length>0)

826{

827offset=document.cookie.indexOf(search)

828if(offset!=-1)

829{

830offset+=search.length

831end=document.cookie.indexOf(";",offset)

832if(end==-1)end=document.cookie.length

833returnunescape(document.cookie.substring(offset,end))

834}

835else

836{

837return;

838}

839}

840}

841};

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新Javascript教程学习
热门Javascript教程学习
编程开发子分类