本帖最后由 nobnobaj 于 2010-05-21 15:23:23 编辑

解决方案 »

  1.   

    var $=function(id){return document.getElementById(id)};
    $.msg=function(str){
        alert(str);
    };
    $.msg("dddd");
      

  2.   


    $.fn.msg=function(str){
        alert(str)
    }或者
    $.extend.msg=function(str){
        alert(str)
    }
      

  3.   

    我可能没写明白我的意思是  对不同对象  $(id)  进行对应的操作  就是类似jquery里
    $('a').bind(....)
    $('b').bind(....)
      

  4.   

    <input id=aaa value="test">
    <script>
    var $=function(id){
        this.o=document.getElementById(id);
    }
    $.prototype.msg=function(str){
         alert(str);
    };var obj= new $('aaa');
    obj.msg("sdf");
    alert(obj.o.value);
    </script>
      

  5.   

    var $=function(id){
       return document.getElementById(id) //这里改成this.dom = document.getElementById(id)
    }
    $.prototype.msg=function(str){
        alert(str)
    }var tmp = new $(id)  //调用的时候使用new构造器函数调用
    tmp.dom//你的这个元素
    tmp.msg() //你拓展的方法
      

  6.   

    to  cj205请问有没有办法 new $(id)封装起来?每次都new一下 比较麻烦
      

  7.   

    http://www.v-ec.com/dh20156/article.asp?id=157看看这个是否对你有帮助
      

  8.   

    你要封装就跟$一样啊var $=function(id){
        this.o=document.getElementById(id);
    }
    $.prototype.msg=function(str){
         alert(str);
    };var $$ = function(id){return new $(id);}$$('aaa').msg("sdf");
    alert($$('aaa').o.value);
      

  9.   

    明白你的意思了,和jq理念一样, 就是对结构进行样式和行为的操作。
    dom.doSomeWorks()。
    可以Google一下 “Query工作原理”<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css">
    </style>
    <title></title></head><body>
    <a onclick="foo1()" id="a1">点击绑定事件</a>
    <div id="test">
    <ul>
         <li>65</li>
         <li id="dd">65</li>
        </ul>
    </div>
    </body>
    <script type="text/javascript">
    var MyQuery = function(selector){
        if ( window == this ) return new MyQuery(selector);
        var dom = document.getElementById(selector);//这里只写byid,更复杂的选择器,建议研究jq源码
        var arr = [dom];
        return this.setArray(arr);
    }
    MyQuery.prototype.setArray = function( arr ) {
            this.length = 0;
            [].push.apply( this, arr );
            return this;
    }
    MyQuery.fn = MyQuery.prototype;
    var $ = MyQuery;//插件扩展
    MyQuery.fn.each = function(method){
        for(var i=0,l=this.length; i<l; i++){
            method.call(this[i],i);
        }
    }
    MyQuery.fn.bind=function(type,eventHandle){//绑定事件
    this.each(function(i){
    if(document.addEventListener){
    this.addEventListener(type,eventHandle,false);
    } else {
    this.attachEvent('on'+type,eventHandle);
    }
    });
    }
    MyQuery.fn.other=function(){
    this.each(function(i){
                 //do other
    });
    }//绑定句柄
    function foo1(){
    $('dd').bind("click",function(){
    alert("绑定")
    })}</script>
    </html>
      

  10.   

    执行 $('aaa').msg('dfdffdf') 提示错误
    这个$ 怎么绑定msg啊$ != $('aaa')
      

  11.   


    var element = function(id) {
    this._element = document.getElementById(id);
    return this;
    }
    element.prototype.msg = function(str) {
    alert(str);
    }
    var $ = function(id) {
    return new element(id);
    }
    $('t').msg('ddd');