<table id="oTable" border="1">
    <thead><tr><th>第一列</th><th>第二列</th><th>第三列</th></tr></thead>
    <tr><td>Cell 1, Row 1</td><td>Cell 2, Row 1</td><td>Cell 3, Row 1</td></tr>
    <tr><td>Cell 1, Row 2</td><td>Cell 2, Row 2</td><td>Cell 3, Row 2</td></tr>
    <tr><td>Cell 1, Row 3</td><td>Cell 2, Row 3</td><td>Cell 3, Row 3</td></tr>
</table>
<input type=button onclick="$('oTable').token('第3列第1列第2列').chang()" value="不断点击变换表单列"><script type=text/javascript>
Function.prototype.$bind = function(object) {
    var This = this;
    return function(ele, tag) {
        This.prototype = object;
        return new This(ele, tag);  
    }
}
$ = function(Id, Tag) {
    var obj = "string"==typeof Id ? document.getElementById(Id) : Id||null;
    if (!Tag) {
        this.length = 1, this[0] = Id ? obj : document;
    } else {
        obj = obj.getElementsByTagName(Tag);
        for (var i = 0; i < obj.length; i ++) {
            this[i] = obj[i];
        }
        this.length = obj.length;
    }
}.$bind({
    token : function(o) {
        this.order = /\d/.test(o) && o.match(/\d+/g);
        return this
    },
    chang : function() {
        this.batch(this, function(This) {
            This.batch(this.rows, function() {
                var arr = this.innerHTML.match(/(?!\s+)[^>]+(?=<)/g);
                    This.batch(this.cells, function(o, i, arg) {
                        this.innerHTML = arr[arg[i] - 1];
                }, This.order)
            })
        })
        return this
    },
    batch : function(object, callback, arg) {
        for (var i = 0; i < object.length; i ++) {
            callback.call(object[i], object, i, arg)
        }
        return object
    }
})
</script>

解决方案 »

  1.   

    “this[0] = Id ? obj : document” ==> this[0] = obj || document
      

  2.   

    充实和梳理了下选择器,主要是当第一个参数为对象[object Obicet]的时候,可扩展自定义程式所需的方法,并以此入口扩展编程。<script type=text/javascript>
    Function.prototype.$bind = function(object) {
        this.prototype = object;
        return function(This) {
            return function(ele, tag) {
                return new This(ele, tag); 
            } 
        }(this)
    }
    $ = function(ele, tag) {
        var obj, eType = this.isType(ele), tType = this.isType(tag);
        if (!tag) {
            if (eType == "[object Function]") {
                this[0] = document;
                this.length = 1;
                this.ready(ele);
            } else if (eType == "[object String]") {
                if (ele.charAt(0) == "#") {
                    obj = document.getElementsByTagName(ele.slice(1));
                    for (var i = 0; i < obj.length; i ++) {
                        this[i] = obj[i];
                        this.length = i + 1;
                    }
                } else {
                    this[0] = document.getElementById(ele);
                    this.length = 1;
                }
            } else if (eType == "[object Object]") {
                    this.extend(this, ele)
            }
        } else {
            if (eType == "[object String]" && tType == "[object String]") {
                obj = document.getElementById(ele).getElementsByTagName(tag);
                for (var i = 0; i < obj.length; i ++) {
                    this[i] = obj[i];
                    this.length = i + 1;
                }
            } else {
                this[0] = document, this.length = 1;
            }
        }
    }.$bind({
        isType : function(o) {
           return Object.prototype.toString.call(o)
        },
        ready : function(callback) {
            window.onload = function(){callback()}
            return this
        },
        batch : function(object, callback, arg) {
            for (var i = 0; i < object.length; i ++) {
                callback.call(object[i], object, i, arg)
            }
            return object
        },
        extend : function(object, property) {
            for (var key in property) {
                object[key] = property[key];
            }
            return object
        },
        token : function(o) {
            this.order = /\d/.test(o) && o.match(/\d+/g);
            return this
        },
        chang : function() {
            this.batch(this, function(This) {
                This.batch(this.rows, function() {
                    var arr = this.innerHTML.match(/(?!\s+)[^>]+(?=<)/g);
                        This.batch(this.cells, function(o, i, arg) {
                            this.innerHTML = arr[arg[i] - 1];
                    }, This.order)
                })
            })
            return this
        }
    })
    //--------------下面的编程方式有点意思-----------------------------
    $({
       look : function(){
            alert("内部批处理(batch)方法:\n" + this.batch)
            return this
       },
       saya : function(o) {
            alert(o)
            return this
       },
       sayb : function(o) {
            alert(o)
            return this
       }
    }).look().saya("who ?").saya("who cares !").sayb("aheheheh....^_^");
    //-------------------------------------------------------------------
    </script><table id="oTable" border="1">
        <thead><tr><th>第一列</th><th>第二列</th><th>第三列</th></tr></thead>
        <tr><td>Cell 1, Row 1</td><td>Cell 2, Row 1</td><td>Cell 3, Row 1</td></tr>
        <tr><td>Cell 1, Row 2</td><td>Cell 2, Row 2</td><td>Cell 3, Row 2</td></tr>
        <tr><td>Cell 1, Row 3</td><td>Cell 2, Row 3</td><td>Cell 3, Row 3</td></tr>
    </table>
    <input type=button onclick="$('oTable').token('第3列第1列第2列').chang()" value="不断点击变换表单列">
      

  3.   

    http://topic.csdn.net/u/20110816/20/c18c770c-75cc-440d-bd08-a6640f9192c0.html