一些JS的操作技巧

    技术2026-10-01  1

    各类浏览器在JS的navigator.userAgent.toLowerCase()中显示结果1 火狐 mozilla/5.0 (windows; u; windows nt 5.1; zh-cn; rv:1.9.0.10) gecko/2009042316 firefox/3.0.10 qqdownload/1.7 2 IE8 mozilla/4.0 (compatible; msie 8.0; windows nt 5.1; trident/4.0; .net clr 2.0.50727; .net clr 3.0.4506.2152; .net clr 3.5.30729) 3 谷歌 mozilla/5.0 (windows; u; windows nt 5.1; en-us) applewebkit/532.0 (khtml, like gecko) chrome/4.0.211.2 safari/532.04 Opera opera/9.80 (windows nt 5.1; u; zh-cn) presto/2.6.30 version/10.615 苹果 mozilla/5.0 (windows; u; windows nt 5.1; zh-cn) applewebkit/533.16 (khtml, like gecko) version/5.0 safari/533.166 IE6 mozilla/4.0 (compatible; msie 6.0; windows nt 5.1;sv1)

    sid 在不同的浏览器中都可用document.cookie获取

    判断对象是否存在 var xx=document.getElementById("m_div"+who); if(xx==null){  return;  }

    在一个div或tb 中,style 中不能同时含有 background-image 和 filter 属性,使用两个div进行嵌套可以解决此问题。

    function olmain(some){var frm=window.document; frm.location=some; frm.Refresh;}function wo(what,w,h){ var woption="toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=no,resizable=no,width="+w+",height="+h; window.open(what,"",woption); return;}

    function ShowDialog(what,w,h){var var1=window.showModalDialog(what, '', "dialogWidth:"+w+" px;dialogHeight:"+h+"px;");    returnproc(var1);}

    function returnproc(var1){ alert(var1);}

    //在回复时function ReturnValue(){ var aForm = johnform01.elements; var fname = aForm.FirstName.value; var lname = aForm.LastName.value; var sHTML = ""; if (fname!="") sHTML+=fname; if (lname!="") sHTML+="*"+lname; if (sHTML==null){window.returnValue = "no"; }else window.returnValue = sHTML; window.close();}

     

    定时 如3秒,执行东东<SCRIPT  Language="JavaScript">var timerID = null;var starttime;function startit(){ starttime=new Date(); showtime();}function showtime(){ now = new Date(); h = (now.getHours()-starttime.getHours()); m = (now.getMinutes() - starttime.getMinutes()); s = (now.getSeconds() - starttime.getSeconds()); mytime= h*3600 + m*60 + s; if (mytime>=3){  var frm=opener.parent.window.frames[2];  frm.location="admin.php?menu=32&code='.$cpindex.'";  frm.Refresh;  window.close(); } timerID = setTimeout("showtime()",1000);}startit();</SCRIPT>

    去除字符串开始空格function trimstr(what){var some=what; if (some.length==0) return ''; while(some.charAt(0)==' '){  some=some.substring(1,some.length); }  return some;}

    // 替换特殊字符function HTMLEncode(text){ text = text.replace(/&/g, "&amp;") ; text = text.replace(/"/g, "&quot;") ; text = text.replace(/</g, "&lt;") ; text = text.replace(/>/g, "&gt;") ; text = text.replace(/'/g, "&#146;") ; text = text.replace(// /g,"&nbsp;"); text = text.replace(//n/g,"<br>"); text = text.replace(//t/g,"&nbsp;&nbsp;&nbsp;&nbsp;"); return text;}

    只能输入数字onKeyPress="event.returnValue=IsDigit()function IsDigit(){  return ((event.keyCode >= 48) && (event.keyCode <= 57));}

    用正则表达式限制只能输入中文:οnkeyup="value=value.replace(/[^/u4E00-/u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/u4E00-/u9FA5]/g,''))"

    用正则表达式限制只能输入数字和英文:οnkeyup="value=value.replace(/[/W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/d]/g,''))"

     if (OneString.search(/^[0-9]+$/)==-1) err+="每页项目数量只能为数字";检测emailfunction chkemai(emailStr){var re=/^[/w.-]+@([0-9a-z][/w-]+/.)+[a-z]{2,3}$/i;if(re.test(emailStr))return true;else{return false;}}<form method="post" action="email_add.asp" οnsubmit="return chkemai(document.getElementById('email_add').value)"> <p>请输入您的E-mail地址: <input type="text" name="email_add"> <input type="submit" value="确定" name="submit"></p> </form>

    数组操作var sysimg=new Array();for (n=0;n<sysimg.length;n++) xxxxxx

    无法选择、复制、粘贴、右键菜单另存为的代码

    <body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" οncοntextmenu="return false" οndragstart="return false" onselectstart ="return false" οnselect="document.selection.empty()" οncοpy="document.selection.empty()" onbeforecopy="return false" οnmοuseup="document.selection.empty()"><noscript><iframe src=*></iframe></noscript>

    格式化字符串输出

    function re_str(){ var numarg=arguments.length; if(numarg<1) return "arguments num too short"; var re = new RegExp("%[0-9-/]","ig"); var arr=arguments[0].match(re); arr.sort(); var arr1=new Array(); var m=0; var str=""; for(var n=0;n<arr.length;n++){  if(arr[n]==str) arr[n]=0;  else{   str=arr[n];   arr1[m]=arr[n];   m++;  }  } if(numarg!=arr1.length+1) return "error of arguments num"; str=arguments[0]; for(n=0;n<arr1.length;n++){  var re=eval("/"+arr1[n]+"/g");  str=str.replace(re,arguments[n+1]); } return str;}

    re_str("a%2%6f%23a%2sd%9fdfk%2","'Some'",23,222);

    最新回复(0)