DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解 >> ajax入門指南(五)
ajax入門指南(五)
編輯:AJAX詳解     
ajax學習入門指南,對AJax初學者應該有所幫助。

Prototype基礎類:
       1. Class.create()
            示例:
                var myClass = Class.create();

      2.  Object.extend(destination,source)
            示例:
                var myClass = Class.create();
                myClass.prototype = {
                initialize: function() {
                 },
                 f1: function() {
                      alert("do f1()");
                 },
                 f2: function() {
                      alert("do f2()");
                 },
                 toString: function() {
                      return "myClass";
                 }
            };
            var mySubClass = Class.create();
            Object.extend(mySubClass.prototype, myClass.prototype);

       3. Object.inspect(object)
            返回目標對象的文字說明,如果對象沒有定義inspect方法,則默認返回object.toString()的值
            示例:
            var myClass = Class.create();
            myClass.prototype = {
                 initialize: function() {
                 },
                 toString: function() {
                  return "myClass";
                 }
            };
            var obj = new myClass();
            alert(Object.inspect(obj));
        4.Function.prototype.bind(object)
            返回一個Function的實例,其結構與當前的Function對象完全相同,只是作用域已經轉移到參數指定的object對象上
            示例:
            var myClass = Class.create();
            myClass.prototype = {
                 initialize: function() {
                 },
                 name: "myClass",
                 f1: function() {
                     alert(this.name + " from f1");
       }
            };
            var myClass2 = Class.create();
            myClass2.prototype = {
                 initialize: function() {
                 },
                 name: "myClass2",
                 f2: function() {
                      alert(this.name + " from f2");
                 }
            };
            var obj = new myClass();
            var obj2 = new myClass2();

            obj2.f2 = obj.f1.bind(obj2);
            obj2.f2(); // 輸出"myClass2 from f1"


        5.Function.prototype.bindAsEventListener
            與bind方法的功能相同,只不過bingAsEvevntListener用於綁定事件.
            示例:
            var Watcher = Class.create();
            Watcher.prototype = {
                     initialize: function(buttonid, message) {
                              this.button = $(buttonid);
                              this.message = message;
                              // 將button的onclick和this對象的showMessage方法綁定起來
                              this.button.onclick =
                               this.showMessage.bindAsEventListener(this);
                     },
                     showMessage: function() {
                          alert(this.message);
                     }
                };
            var watcher = new Watcher('btn', 'clicked');

        6.PeriodicalExecuter類
            創建PeriodicalExecuter類的實例將會周期性地調用指定的方法
            function setTime() {
                 $('divTime').innerHtml = (new Date()).toLocaleString();
            }
            new PeriodicalExecuter(setTime, 1);
            第一個參數:調用的方法;第二個參數:間隔多少秒
字符串處理(String對象擴展)
           1.String .prototype.gsub(pattern,replacement)
                將字符串中所有正則表達式匹配的部分替換成指定的字符串
                pattern:正則表達式
                replacement:用作替換的字符串
                示例:
                var str = "this is a test test";
                //輸出"this is a new new"
                alert(str.gsub(/test/,"new"));
               
            2.String.prototype.truncate(length,truncation)
                將字符串截斷
                length:截斷後字符串的長度,默認值為30
                trancation:截斷字符串時,替代尾部的字符串,默認是"..."
                示例:
                 var str = "this is a test test";
                //輸出"this is ..."
                  alert(str.truncate(10));
                //輸出"this is a t..."
                alert(str.truncate(14));
                //輸出"this is***"
                alert(str.truncate(10,"***"));


            3.String.prototype.strip()
                刪除字符串前後的空白字符
                示例:
                var str="this is a test test  ";
                alert(str.strip().length);//19
                alert(str.length);//21

            4.String.prototype.stripTags()
                移除字符串中所有的Html和XML標簽
                示例:
                var str = "<table><tr><td>stripTagsDemo</td></tr></table>";
               alert(str.stripTags());//輸出"stripTagsDemo"

            5.String.prototype.stripScripts()
                移除字符串所有的<script></script>腳本標記內容
                示例:
                var str = "this is a test<script>alert('ok')<"+"/script>";
               alert(str.stripScripts());//輸出"this is a test"
6.String.prototype.evalScripts()
                執行在字符串中找到的所有腳本
                var str = "this is a test<script>alert('ok')<"+"/script>";
               str.evalScripts();//輸出"ok"

            7.String.prototype.escapeHtml()
                將字符串中的所有Html標記進行轉義
                示例:
                var str = "<table><tr><td>stripTagsDemo</td></tr></table>";
               alert(str.escapeHtml());

            8.String.prototype.unescapeHtml()
                執行與escapeHtml()相反的操作


            9.String.prototype.toQueryParams()
                將查詢字符串轉化為一個聯合數組
                示例:
                var str = "a=1&b=2&c=3"
                var arr = str.toQueryParams();
                for(i in arr){
                     alert(arr[i]);//輸出1,2,3
               }

            10.String.prototype.toArray()
                把字符串轉換成字符數組
                示例:
                var str = "test"; 
                //依次輸出"t,e,s,t"
               str.toArray().each(
                    function(item){
                       alert(item);
                   }
               );

            11.String.prototype.extractScripts()
                從字符串中提取出所有的<script>腳本,以字符串數組的形式返回
                示例:
                var str = "this is a test<script>alert('ok')<"+"/script>";
               str += str;
                //輸出兩遍alert('ok')
               str.extractScripts().each(
                   function(item){
                       alert(item); 
      }
               );
 

            12.String.prototype.camelize()
                將一個以連字符連接的字符串轉換成一個遵循駱駝命名法的字符串
                示例:
                var str = "this-is-a-test";  
                //輸出"thisIsATest"
               alert(str.camelize());

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved