Javascript常用小技巧1.基本控件的使用控件名.value控件名.value控件名[i].checked.valueforeachobin控件名ifob.checkedthenwindow.alertob.valuenext控件名.checked.valueSelect1Select1Select2Select3Select4Select1Select2Select3Select4单行多行多选添加选项ExpList.add(newOption(NewOption,3));删除选项ExpList.remove(ExpList.selectedIndex);设置当前选项位置ExpList.selectedIndex=ExpList.length-1;循环整个选项并判for(i=0;iExpList.options.length;i++){if(ExpList.options[i].selected){//ExpList.options[i].text;//ExpList.options[i].value;}断是否被选择}单个选项的值ExpList.options[i].text;ExpList.options[i].value;spanid=SomePlace1/spandivid=SomePlace1/div重新设计包含的htmlSomePlace1.innerHTML=任意html字符串让该空间的内容消失SomePlace1.style.display=none;让该空间的内容显示SomePlace1.style.display=block;鼠标滑过出现提示title=Helpinputtype=hiddenname=MyValueNamevalue=123控件名.value跟随事件Event(声明方式特殊)divid=hello1style=position:absolute太过分了!吃饭不叫我/divSCRIPTfor=documentEVENT=onmousemovedocument_onmousemove();functiondocument_onmousemove(){hello1.style.left=event.x+10;hello1.style.top=event.y+10;}/SCRIPT返回2.让TextArea自动换行使用TextArea的Wrap属性Wrapmaybesettooneofthefollowingvalues:oOFF-Default,linesarenotwrapped.oPHYSICAL-Wraplinesandplacenewlinecharacterswherethelinewraps.oVIRTUAL-Wraplinesonthescreen,butreceivethemasoneline.ASP中手动换行用:replace(rs(A),/br,VBCRLF)str=request.querystring(text1)str=Replace(str,Chr(32),)'把空格换成标志str=Replace(str,vbCrLf,br)'把回车换行符换成br标志调出时再转过来就好了返回3.让TextArea支持Table键SCRIPTLANGUAGE=JavaScript!--functiontestTab(){varsel=document.selection.createRange();varmytext=sel.text;vari,j,k;if(event.shiftKey&&event.keyCode==9){arr=mytext.split(String.fromCharCode(13,10))mytext=for(k=0;karr.length;k++){for(j=1;j=4;j++){if(arr[k].substr(0,1)==\u0020){arr[k]=arr[k].slice(1)}}mytext+=arr[k]+((k==(arr.length-1))?:String.fromCharCode(13,10));}with(sel){sel.text=mytext;collapse(true)moveEnd(character,0)moveStart(character,(mytext.length)*-1)select()}window.event.cancelBubble=true;event.returnValue=false;return;}if(event.keyCode==9){arr=mytext.split(String.fromCharCode(13,10))mytext=for(j=0;jarr.length;j++){mytext+=\u0020\u0020\u0020\u0020+arr[j]+((j==(arr.length-1))?:String.fromCharCode(13,10));}with(sel){sel.text=mytext;collapse(true)moveEnd(character,0)moveStart(character,(mytext.length-4)*-1)select()}window.event.cancelBubble=true;event.returnValue=false;return;}}//--/SCRIPT返回4.复制数据到剪贴板JM_cc(txt)window.clipboardData.setData(Text,txt);global.focus();document.execCommand('Paste');window.clipboardData.setData(Text,old);返回5.得到当前选中的文本varsel=document.selection.createRange();varmytext=sel.text;//当前选中的文本vare=event.srcElement;//设置文本为选中varr=e.createTextRange();r.moveStart('character',e.value.length);r.collapse(true);r.select();返回6.客户端脚本的基本对象navigatorscreenwindowohistoryolocationoframes[];Frame浏览器对象屏幕对象窗口对象历史对象地址对象框架对象odocumentanchors[];links[];Linkapplets[]embeds[]forms[];FormButtonCheckboxelements[];ElementHiddenPasswordRadioResetSelectoptions[];OptionSubmitTextTextareaimages[];Imagemore文档对象连接对象Java小程序对象插件对象表单对象按钮对象复选框对象表单元素对象隐藏对象密码输入区对象单选域对象重置按钮对象选择区(下拉菜单、列表)对象选择项对象提交按钮对象文本框对象多行文本输入区对象图片对象返回7.保护自己编写的HTML和脚本的方法(1).oncontextmenu=window.event.returnValue=false将彻底屏蔽鼠标右键tableborderoncontextmenu=return(false)tdno/table可用于Table(2).bodyonselectstart=returnfalse取消选取、防止复制(3).onpaste=returnfalse不准粘贴(4).oncopy=returnfalse;oncut=returnfalse;防止复制(5).防止被人frameSCRIPTLANGUAGE=JAVASCRIPT!--if(top.location!=self.location)top.location=self.location;//--/SCRIPT(6).永远都会带着框架scriptlanguage=JavaScript!--if(window==top)top.location.href=frames.htm;//frames.htm为框架网页//--/script(7).网页将不能被另存为noscriptiframesrc=*.html/iframe/noscript返回8.IE地址栏前换成自己的图标linkrel=ShortcutIconhref=favicon.ico返回9.可以在收藏夹中显示出你的图标linkrel=Bookmarkhref=favicon.ico返回10.关闭输入法inputstyle=ime-mode:disabled返回11.直接查看源代码inputtype=buttonvalue=查看网页源代码onclick=window.location='view-source:'+'返回12.在Javascript中定义一个对象(属性,方法)functionpasta(grain,width,hasEgg){this.grain=grain;this.width=width;this.hasEgg=hasEgg;this.toString=pastaToString;}functionpastaToString(){returnGrain:+this.grain+\n+Width:+this.width+\n+Egg?:+Boolean(this.hasEgg);}varP1=newpasta(3,3,false);返回13.取得控件的绝对位置//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返回14.光标是停在文本框文字的最后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.判断上一页的来源asp:request.servervariables(HTTP_REFERER)javascript:document.referrer返回16.最小化、最大化、关闭窗口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/OBJECTinputt