Javascript技術技巧大全(一)

來源:互聯網
上載者:User
javascript|技巧 //各種尺寸 s  +=  "\r\n網頁可見地區寬:"+  document.body.clientWidth;  
s  +=  "\r\n網頁可見地區高:"+  document.body.clientHeight;  
s  +=  "\r\n網頁可見地區高:"+  document.body.offsetWeight  +"  (包括邊線的寬)";  
s  +=  "\r\n網頁可見地區高:"+  document.body.offsetHeight  +"  (包括邊線的寬)";  
s  +=  "\r\n網頁本文全文寬:"+  document.body.scrollWidth;  
s  +=  "\r\n網頁本文全文高:"+  document.body.scrollHeight;  
s  +=  "\r\n網頁被捲去的高:"+  document.body.scrollTop;  
s  +=  "\r\n網頁被捲去的左:"+  document.body.scrollLeft;  
s  +=  "\r\n網頁本文部分上:"+  window.screenTop;  
s  +=  "\r\n網頁本文部分左:"+  window.screenLeft;  
s  +=  "\r\n螢幕解析度的高:"+  window.screen.height;  
s  +=  "\r\n螢幕解析度的寬:"+  window.screen.width;  
s  +=  "\r\n螢幕可用工作區高度:"+  window.screen.availHeight;  
s  +=  "\r\n螢幕可用工作區寬度:"+  window.screen.availWidth;  

    //過濾數字 <input type=text onkeypress="return event.keyCode>=48&&event.keyCode<=57||(this.value.indexOf('.')<0?event.keyCode==46:false)" onpaste="return !clipboardData.getData('text').match(/\D/)" ondragenter="return false">

    //特殊用途 <input type=button value=匯入收藏夾 onclick="window.external.ImportExportFavorites(true,'http://localhost');">
    <input type=button value=匯出收藏夾 onclick="window.external.ImportExportFavorites(false,'http://localhost');">
    <input type=button value=整理收藏夾 onclick="window.external.ShowBrowserUI('OrganizeFavorites', null)">
    <input type=button value=語言設定   onclick="window.external.ShowBrowserUI('LanguageDialog', null)">
    <input type=button value=加入收藏夾 onclick="window.external.AddFavorite('http://www.google.com/', 'google')">
    <input type=button value=加入到頻道 onclick="window.external.addChannel('http://www.google.com/')">
    <input type=button value=加入到頻道 onclick="window.external.showBrowserUI('PrivacySettings',null)">

    //不緩衝 <META HTTP-EQUIV="pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
    <META HTTP-EQUIV="expires" CONTENT="0">


    //正則匹配

    匹配中文字元的Regex: [\u4e00-\u9fa5]
    匹配雙位元組字元(包括漢字在內):[^\x00-\xff]
    匹配空行的Regex:\n[\s| ]*\r
    匹配HTML標記的Regex:/<(.*)>.*<\/\1>|<(.*) \/>/ 
    匹配首尾空格的Regex:(^\s*)|(\s*$)(像vbscript那樣的trim函數)
    匹配Email地址的Regex:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
    匹配網址URL的Regex:http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
    以下是例子:
    利用Regex限制網頁表單裡的文字框輸入內容:
    用Regex限制只能輸入中文:onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))"
    1.用Regex限制只能輸入全形字元: onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))"
    2.用Regex限制只能輸入數字:onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"
    3.用Regex限制只能輸入數字和英文:onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"

    //消除映像工具列 <IMG SRC="mypicture.jpg" HEIGHT="100px" WIDTH="100px" GALLERYIMG="false"> 
    or
    <head>
    <meta http-equiv="imagetoolbar" content="no">
    </head>

    //無提示關閉

    function Close()
    {
     var ua=navigator.userAgent
     var ie=navigator.appName=="Microsoft Internet Explorer"?true:false
     if(ie)
     {
          var IEversion=parseFloat(ua.substring(ua.indexOf("MSIE ")+5,ua.indexOf(";",ua.indexOf("MSIE "))))
      if(IEversion< 5.5)
      {
       var str  = '<object id=noTipClose classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">'
           str += '<param name="Command" value="Close"></object>';
           document.body.insertAdjacentHTML("beforeEnd", str);
           document.all.noTipClose.Click();
      }
          else
      {
           window.opener =null;
           window.close();
          }
       }
     else
     {
      window.close()
       }
    }

    //取得控制項得絕對位置(1)

    <script language="javascript"> 
    function getoffset(e)
    { 
     var t=e.offsetTop; 
     var l=e.offsetLeft; 
     while(e=e.offsetParent)
     { 
      t+=e.offsetTop; 
      l+=e.offsetLeft; 
     } 
     var rec = new Array(1);
     rec[0]  = t;
     rec[1] = l;
     return rec
    } 
    </script>

    //獲得控制項的絕對位置(2)

    oRect = obj.getBoundingClientRect();
    oRect.left
    oRect.

    //最小化,最大化,關閉 <object id=min classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11"> 
    <param name="Command" value="Minimize"></object> 
    <object id=max classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11"> 
    <param name="Command" value="Maximize"></object> 
    <OBJECT id=close classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11"> 
    <PARAM NAME="Command" value="Close"></OBJECT> 
    <input type=button value=最小化 onclick=min.Click()> 
    <input type=button value=最大化 onclick=max.Click()> 
    <input type=button value=關閉 onclick=close.Click()> 

    //游標停在文字最後

    <script language="javascript">
    function cc()
    {
     var e = event.srcElement;
     var r =e.createTextRange();
     r.moveStart('character',e.value.length);
     r.collapse(true);
     r.select();
    }
    </script>
    <input type=text name=text1 value="123" onfocus="cc()">

    //頁面進入和退出的特效

    進入頁面<meta http-equiv="Page-Enter" content="revealTrans(duration=x, transition=y)">
    推出頁面<meta http-equiv="Page-Exit" content="revealTrans(duration=x, transition=y)"> 
    這個是頁面被載入和調出時的一些特效。duration表示特效的期間,以秒為單位。transition表示使
    用哪種特效,取值為1-23:
      0 矩形縮小 
      1 矩形擴大 
      2 圓形縮小
      3 圓形擴大 
      4 下到上重新整理 
      5 上到下重新整理
      6 左到右重新整理 
      7 右到左重新整理 
      8 豎百葉窗
      9 橫百葉窗 
      10 錯位橫百葉窗 
      11 錯位豎百葉窗
      12 點擴散 
      13 左右到中間重新整理 
      14 中間到左右重新整理
      15 中間到上下
      16 上下到中間 
      17 右下到左上
      18 右上到左下 
      19 左上到右下 
      20 左下到右上
      21 橫條 
      22 豎條


    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.