您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 市场营销 > 55种Javascript网页常用小技巧
55种Javascript网页常用小技巧1.oncontextmenu=window.event.returnValue=false将彻底屏蔽鼠标右键tableborderoncontextmenu=return(false)tdno/table可用于Table2.bodyonselectstart=returnfalse取消选取、防止复制3.onpaste=returnfalse不准粘贴4.oncopy=returnfalse;oncut=returnfalse;防止复制5.linkrel=ShortcutIconhref=favicon.icoIE地址栏前换成自己的图标6.linkrel=Bookmarkhref=favicon.ico可以在收藏夹中显示出你的图标7.inputstyle=ime-mode:disabled关闭输入法8.永远都会带着框架scriptlanguage=JavaScript!--if(window==top)top.location.href=frames.htm;//frames.htm为框架网页//--/script9.防止被人frameSCRIPTLANGUAGE=JAVASCRIPT!--if(top.location!=self.location)top.location=self.location;//--/SCRIPT10.网页将不能被另存为noscriptiframesrc=*.html/iframe/noscript11.inputtype=buttonvalue=查看网页源代码onclick=window.location=view-source:+=javascript:if(confirm(确实要删除吗?))location=boos.asp?&areyou=删除&page=1删除/a13.取得控件的绝对位置//Javascriptscriptlanguage=JavascriptfunctiongetIE(e){vart=e.offsetTop;varl=e.offsetLeft;while(e=e.offsetParent){t+=e.offsetTop;l+=e.offsetLeft;}alert(top=+t+/nleft=+l);}/script//VBScriptscriptlanguage=VBScript!--functiongetIE()dimt,l,a,bseta=document.all.img1t=document.all.img1.offsetTopl=document.all.img1.offsetLeftwhilea.tagNameBODYseta=a.offsetParentt=t+a.offsetTopl=l+a.offsetLeftwendmsgboxtop=&t&chr(13)&left=&l,64,得到控件的位置endfunction--/script14.光标是停在文本框文字的最后scriptlanguage=javascriptfunctioncc(){vare=event.srcElement;varr=e.createTextRange();r.moveStart(character,e.value.length);r.collapse(true);r.select();}/scriptinputtype=textname=text1value=123onfocus=cc()15.判断上一页的来源javascript:document.referrer16.最小化、最大化、关闭窗口objectid=hh1classid=clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11paramname=Commandvalue=Minimize/objectobjectid=hh2classid=clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11paramname=Commandvalue=Maximize/objectOBJECTid=hh3classid=clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11PARAMNAME=CommandVALUE=Close/OBJECTinputtype=buttonvalue=最小化onclick=hh1.Click()inputtype=buttonvalue=最大化onclick=hh2.Click()inputtype=buttonvalue=关闭onclick=hh3.Click()本例适用于IE17.屏蔽功能键Shift,Alt,Ctrlscriptfunctionlook(){if(event.shiftKey)alert(禁止按Shift键!);//可以换成ALTCTRL}document.onkeydown=look;/script18.网页不会被缓存METAHTTP-EQUIV=pragmaCONTENT=no-cacheMETAHTTP-EQUIV=Cache-ControlCONTENT=no-cache,must-revalidateMETAHTTP-EQUIV=expiresCONTENT=Wed,26Feb199708:21:57GMT或者METAHTTP-EQUIV=expiresCONTENT=019.怎样让表单没有凹凸感?inputtype=textstyle=border:1solid#000000或inputtype=textstyle=border-left:none;border-right:none;border-top:none;border-bottom:1solid#000000/textarea20.divspan&layer的区别?div(division)用来定义大段的页面元素,会产生转行span用来定义同一行内的元素,跟div的唯一区别是不产生转行layer是ns的标记,ie不支持,相当于div21.让弹出窗口总是在最上面:bodyonblur=this.focus();22.不要滚动条?让竖条没有:bodystyle=overflow:scroll;overflow-y:hidden/body让横条没有:bodystyle=overflow:scroll;overflow-x:hidden/body两个都去掉?更简单了bodyscroll=no/body23.怎样去掉图片链接点击后,图片周围的虚线?ahref=#onFocus=this.blur()imgsrc=logo.jpgborder=0/a24.电子邮件处理提交表单formname=form1method=postaction=mailt****@***.comenctype=text/plaininputtype=submit/form25.在打开的子窗口刷新父窗口的代码里如何写?window.opener.location.reload()26.如何设定打开页面的大小bodyonload=top.resizeTo(300,200);打开页面的位置bodyonload=top.moveBy(300,200);27.在页面中如何加入不是满铺的背景图片,拉动页面时背景图不动STYLEbody{background-image:url(logo.gif);background-repeat:no-repeat;background-position:center;background-attachment:fixed}/STYLE28.检查一段字符串是否全由数字组成scriptlanguage=Javascript!--functioncheckNum(str){returnstr.match(//D/)==null}alert(checkNum(1232142141))alert(checkNum(123214214a1))//--/script29.获得一个窗口的大小document.body.clientWidth;document.body.clientHeight30.怎么判断是否是字符if(/[^/x00-/xff]/g.test(s))alert(含有汉字);elsealert(全是字符);31.TEXTAREA自适应文字行数的多少textarearows=1name=s1cols=27onpropertychange=this.style.posHeight=this.scrollHeight/textarea32.日期减去天数等于第二个日期scriptlanguage=Javascriptfunctioncc(dd,dadd){//可以加上错误处理vara=newDate(dd)a=a.valueOf()a=a-dadd*24*60*60*1000a=newDate(a)alert(a.getFullYear()+年+(a.getMonth()+1)+月+a.getDate()+日)}cc(12/23/2002,2)/script33.选择了哪一个RadioHTMLscriptlanguage=vbscriptfunctioncheckme()foreachobinradio1ifob.checkedthenwindow.alertob.valuenextendfunction/scriptBODYINPUTname=radio1type=radiovalue=stylecheckedStyleINPUTname=radio1type=radiovalue=barcodeBarcodeINPUTtype=buttonvalue=checkonclick=checkme()/BODY/HTML34.脚本永不出错SCRIPTLANGUAGE=JavaScript!--HidefunctionkillErrors(){returntrue;}window.onerror=killErrors;//--/SCRIPT35.ENTER键可以让光标移到下一个输入框inputonkeydown=if(event.keyCode==13)event.keyCode=936.检测某个网站的链接速度:把如下代码加入body区域中:scriptlanguage=Javascripttim=1setInterval(tim++,100)b=1varautourl=newArray()autourl[1]=[2]=javacool.3322.netautourl[3]=[4]=[5]=(){document.write(formname=autof)for(vari=1;iautourl.length;i++)document.write(inputtype=textname=txt+i+size=10value=测试中……=》inputtype=textname=url+i+size=40=》inputtype=buttonvalue=GOonclick=window.open(this.form.url+i+.value
本文标题:55种Javascript网页常用小技巧
链接地址:https://www.777doc.com/doc-5839980 .html