解决方案 »

  1.   

    $('#id').test('string') 
      

  2.   


    你没明白我说的是什么! 我说的不是用jquery 是自己写一个$().test()去实现上面的功能
      

  3.   

    jquery写插件有3种方式:
    1、全局插件,例如你的$.test();
    2、对象方法插件,这个比较常见;
    3、选择器插件,用的比较少,建议去研究研究
      

  4.   


    $.test()这个真心不方便,因为打算封装好以后就不碰DOM命令了,但是$.test()这么一写,因为在test方法里还要写DOM命令所以觉得特别不方便。
    所以想知道jquery那种 $().X() 是怎么实现的.
      

  5.   

    类似这样<script type="text/javascript">
    function ClassObj(id)
    {
    this.dom = document.getElementById(id);
    }
    ClassObj.prototype.html = function (e)
    {
    if(e)
    {
    this.dom.innerHTML = e;
    return this;
    }
    else
    return this.dom.innerHTML;
    }var $ = function (id)
    {
    return new ClassObj(id);
    };
    </script><p id='asd'>求助</p>
    <input type="button" value="xxxxxxxxxxx" onclick="$('asd').html('123456')" />
      

  6.   

    $.fn.test= funciton(){}
    这个直接修改 $的propotype,以后所有的$().就都有这个方法了。
    不过不建议这样添加,主要是多人开发时常会有命名冲突,或者有一个统一的添加fn的决策机制以防止此类现象。
      

  7.   


    js新手,求详细注释,谢谢,我对对象的概念不是很理解. propotype这个东西,上网查过,不过各位大神说的昏天暗地的,实在不理解,求个简单易懂的解释,谢谢
      

  8.   


    (function(window){
    _$ = window.$;
    $ = function(o){
        if(Boolean(o) == false) 
            return event.srcElement;
        else
            return typeof(o) == "string"?document.getElementById(o):o;
    };
    $.test=function(str){
        document.write('郁闷蛋疼'+str);
    }
    }(window))
    上面的$.test()可以执行;但是换成$().test()就不行 ,求解
      

  9.   


    (function(window){
    _$ = window.$;
    $ = function(o){
        if(Boolean(o) == false) 
            return event.srcElement;
        else
            return typeof(o) == "string"?document.getElementById(o):o;
    };
    $.test=function(str){
        document.write('郁闷蛋疼'+str);
    }
    }(window))
    上面的$.test()可以执行;但是换成$().test()就不行 ,求解
    不是说了么。$()函数要返回对象的实例
      

  10.   

    你要干活,就去看jQuery 的API,你要学习,就去用原生的javascript,你用jQuery 去学习原生的javascript,自己找累,jQuery 里有很多高级的javascript运用方法,你基础没打好就想去了解jQuery 的原理,自己加大了学习的复杂性。propotype,就是javascript的原生类实现机制,用于封装类的公共方法,主要是共享内存,多个类实例共同使用一段程序片段,以节约内存。function  A() {
        this.name= "A";
        this.show = function(){
            alert(this.name);
        }
    }function B (){
        this.name= "B";
    }
    B.prototype.show = function(){
        alert(this.name);
    }在这里,你new A(); new B();他们的show()方法都是一样的,不过一般推荐的写法是第二个。好处一个是节约内存,另一个扩展方便,你可以写个基本类,然后在别的文件里对这个类进行扩展,而且可以直接修改javascript内置对象Array,String,Function等的prototype 进行扩展。
    当然对javascript内置对象的prototype 扩展有两种对立的思路,一种认为这种做法污染了原生的javascript,容易造成版本控制混乱,倾向于用别的方法实现,代表就是jQuery,另一种是认为合理的扩展是必须的,简化API,让程序更好理解,代表是Mootools.
      

  11.   

    function $(id){
    if(typeof id == 'undefined'){
    alert('must a string');
    }
    if(this == window){
    return new $(id);
    }

    this.dom = document.getElementById(id);
    }$.prototype = {
    test : function(text){
    this.dom.innerText = text;
    }
    }
      

  12.   

    上面是老jQuery的写法。下面是新jQuery的写法。
    function $(id){
    if(typeof id == 'undefined'){
    alert('must a string');
    }
    return new $.prototype.init(id);
    }$.prototype = {
    test : function(text){
    this.dom.innerText = text;
    },
    init : function(id){
    this.dom = document.getElementById(id);
    }
    }
    $.prototype.init.prototype = $.prototype;
      

  13.   


    return new $(id);
    把自己作为对象返回该如何理解?  在用$().test()的时候  相当于(new $(id)).test()? 然后又返回一个对象(new (new $(id))).test()?   不太明白
      

  14.   


    return new $(id);
    把自己作为对象返回该如何理解?  在用$().test()的时候  相当于(new $(id)).test()? 然后又返回一个对象(new (new $(id))).test()?   不太明白if在那摆着,哪能让你new两次啊。
    伦家$的意思是,你要是调用我的时候,用的是$(id)这种形式,就给你修正一下,论家本来要用new $(id)。不排除某些人很懒,忘记加new。so,给你来了个if,然后给你加上new。如果你就是用的new,那if根本进不去。
      

  15.   


    return new $(id);
    把自己作为对象返回该如何理解?  在用$().test()的时候  相当于(new $(id)).test()? 然后又返回一个对象(new (new $(id))).test()?   不太明白if在那摆着,哪能让你new两次啊。
    伦家$的意思是,你要是调用我的时候,用的是$(id)这种形式,就给你修正一下,论家本来要用new $(id)。不排除某些人很懒,忘记加new。so,给你来了个if,然后给你加上new。如果你就是用的new,那if根本进不去。
    然后呢? new完的对象返回给谁了? test里面用的是dom属性,根本没用到那个对象阿。
      

  16.   

    没人接。他不需人接,因为直接用连写了。<p id='asd'>求助</p><script>
    function $(id){
    this.dom = document.getElementById(id)
    }$.prototype = {
    changeText : function(){
    this.dom.innerText = 'fuck ?';
    return this;
    },
    changeStyle : function(){
    this.dom.style.color = 'red';
    return this;
    }
    }
    new $('asd').changeText().changeStyle();
    </script>
      

  17.   


    var divDrag = {
        o: null,
        init: function (divHeader, DivContent) {
            divHeader.onmousedown = this.start;
            DivContent.onmousedown = this.start;
        },
        start: function (e) {
            var o;
            divDrag.o = o = document.getElementById("tanchu_warrper");
            e = divDrag.fixEvent(e);
            e.preventDefault && e.preventDefault();
            o.x = e.clientX - divDrag.o.offsetLeft + 239;
            o.y = e.clientY - divDrag.o.offsetTop + 150;
            document.onmousemove = divDrag.move;
            document.onmouseup = divDrag.end;
        },
        move: function (e) {
            e = divDrag.fixEvent(e);
            var oLeft, oTop;
            oLeft = e.clientX - divDrag.o.x + 239;
            oTop = e.clientY - divDrag.o.y + 150;
            divDrag.o.style.left = oLeft + 'px';
            divDrag.o.style.top = oTop + 'px';
            divleft = oLeft * 1;
            divtop = oTop * 1;
        },
        end: function (e) {
            e = divDrag.fixEvent(e);
            divDrag.o = document.onmousemove = document.onmouseup = null;
        },
        fixEvent: function (e) {
            if (!e) {
                e = window.event;
                e.target = e.srcElement;
                e.layerX = e.offsetX;
                e.layerY = e.offsetY;
            }
            return e;
        }
    }
    divDrag.init(div, divBox); = -= 像这种 封装方法就可以实现 
      

  18.   

    这个能看懂但是到了匿名函数里就懵了,看网上说要把对象挂载到window对象上,比如$,网上说window.$挂载这个对象,但是$里面又有别的属性,换句话说一级一级属性把我弄得很糊涂,不知道如何把子属性和父属性联系起来。 
      

  19.   

    function $(id){
        if(typeof id == 'undefined'){
            alert('must a string');
        }
        if(this == window){
            return new $(id);
        }
         
        this.dom = document.getElementById(id);
    }
     
    $.prototype = {
            test : function(text){
                this.dom.innerText = text;
            }
    }
      

  20.   

    没有什么不好理解的
    你只需把 $(id)
    当做 document.getElementById(id) 的缩写
    就可以了
    于是 o = $(id) 后 o 就具有了 id 为 'id' 的 dom 对象的一切属性和方法了
      

  21.   

    嗯 没错 我就是这么想的,不过上面几位说的要把$()当作一个对象返回,我就不太明白了。因为按照上面几个朋友的代码,返回的$()并没有用到。用到的是this.dom这个属性啊。
    function $(id){
    if(typeof id == 'undefined'){
    alert('must a string');
    }
    if(this == window){
    return new $(id);
    }

    this.dom = document.getElementById(id);
    }$.prototype = {
    test : function(text){
    this.dom.innerText = text;
    }
    }但是我的代码里 不返回对象,却不能实现$(id).test(str)这种,只能这样用$.test(str),但是这样就没法用$()获得要响应事件的标签,只能又去写document......了。 总之单独使用$()可以,连用只能$.test()。(function(window){
    _$ = window.$;
    $ = function(o){
        if(Boolean(o) == false) 
            return event.srcElement;
        else
            return typeof(o) == "string"?document.getElementById(o):o;
    };
    $.test=function(str){
        document.write('郁闷蛋疼'+str);
    }
    }(window))
      

  22.   

    嗯 没错 我就是这么想的,不过上面几位说的要把$()当作一个对象返回,我就不太明白了。因为按照上面几个朋友的代码,返回的$()并没有用到。用到的是this.dom这个属性啊。
    function $(id){
    if(typeof id == 'undefined'){
    alert('must a string');
    }
    if(this == window){
    return new $(id);
    }

    this.dom = document.getElementById(id);
    }$.prototype = {
    test : function(text){
    this.dom.innerText = text;
    }
    }但是我的代码里 不返回对象,却不能实现$(id).test(str)这种,只能这样用$.test(str),但是这样就没法用$()获得要响应事件的标签,只能又去写document......了。 总之单独使用$()可以,连用只能$.test()。(function(window){
    _$ = window.$;
    $ = function(o){
        if(Boolean(o) == false) 
            return event.srcElement;
        else
            return typeof(o) == "string"?document.getElementById(o):o;
    };
    $.test=function(str){
        document.write('郁闷蛋疼'+str);
    }
    }(window))
    LZ要先了解下怎么创建对象,你的这个实例不是对象,你的$和$.test只是两个独立的函数,只不过函数名使用了命名空间而已。 可能大家给的例子有点复杂,看看下面function Class(id)
    {
    //对象的构造函数
    }
    Class.prototype.mt = function ()
    {
    //对象的一个方法
    alert(34);
    }//这个Class对象的使用方法有
    //方法1:
    var obj = new Class(123);
    obj.mt();//方法2:
    new Class(123).mt();//方法3:
    var $ = function (id)
    {
    return new Class(id);
    };
    $(123).mt();方法3就是你想要有吧
      

  23.   

    嗯 没错 我就是这么想的,不过上面几位说的要把$()当作一个对象返回,我就不太明白了。因为按照上面几个朋友的代码,返回的$()并没有用到。用到的是this.dom这个属性啊。
    function $(id){
    if(typeof id == 'undefined'){
    alert('must a string');
    }
    if(this == window){
    return new $(id);
    }

    this.dom = document.getElementById(id);
    }$.prototype = {
    test : function(text){
    this.dom.innerText = text;
    }
    }但是我的代码里 不返回对象,却不能实现$(id).test(str)这种,只能这样用$.test(str),但是这样就没法用$()获得要响应事件的标签,只能又去写document......了。 总之单独使用$()可以,连用只能$.test()。(function(window){
    _$ = window.$;
    $ = function(o){
        if(Boolean(o) == false) 
            return event.srcElement;
        else
            return typeof(o) == "string"?document.getElementById(o):o;
    };
    $.test=function(str){
        document.write('郁闷蛋疼'+str);
    }
    }(window))
    LZ要先了解下怎么创建对象,你的这个实例不是对象,你的$和$.test只是两个独立的函数,只不过函数名使用了命名空间而已。 可能大家给的例子有点复杂,看看下面function Class(id)
    {
    //对象的构造函数
    }
    Class.prototype.mt = function ()
    {
    //对象的一个方法
    alert(34);
    }//这个Class对象的使用方法有
    //方法1:
    var obj = new Class(123);
    obj.mt();//方法2:
    new Class(123).mt();//方法3:
    var $ = function (id)
    {
    return new Class(id);
    };
    $(123).mt();方法3就是你想要有吧

    这回明白了
    非常感谢
    Class.prototype 是给class添加属性的吧 
    class.prototype={}