jquery $().click()......这个是怎么实现的

jiangbai333 2013-11-29 04:58:53
把上面的问题简单化如下:
假设自己写了一个js文件, 里面的这个$('id').test('string') 执行完 会把id='id'的标签的文本换成'string'。

例如:点击a标签把p的文本换成123456;

<p id='asd'>求助</p>
<a onclick = $('asd').test('123456')><a>


以上例子中$('id').test('string') 该怎么实现? 我写的只能$.test()。写成$().test()不行,不好使。
...全文
538 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiangbai333 2013-12-02
  • 打赏
  • 举报
回复
引用 25 楼 xuzuning 的回复:
没有什么不好理解的 你只需把 $(id) 当做 document.getElementById(id) 的缩写 就可以了 于是 o = $(id) 后 o 就具有了 id 为 'id' 的 dom 对象的一切属性和方法了
嗯 没错 我就是这么想的,不过上面几位说的要把$()当作一个对象返回,我就不太明白了。因为按照上面几个朋友的代码,返回的$()并没有用到。用到的是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))
jiangbai333 2013-12-02
  • 打赏
  • 举报
回复
引用 27 楼 jslang 的回复:
[quote=引用 26 楼 jiangbai333 的回复:] [quote=引用 25 楼 xuzuning 的回复:] 没有什么不好理解的 你只需把 $(id) 当做 document.getElementById(id) 的缩写 就可以了 于是 o = $(id) 后 o 就具有了 id 为 'id' 的 dom 对象的一切属性和方法了
嗯 没错 我就是这么想的,不过上面几位说的要把$()当作一个对象返回,我就不太明白了。因为按照上面几个朋友的代码,返回的$()并没有用到。用到的是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))
[/quote] 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就是你想要有吧 [/quote] 这回明白了 非常感谢 Class.prototype 是给class添加属性的吧 class.prototype={ }
天际的海浪 2013-12-02
  • 打赏
  • 举报
回复
引用 26 楼 jiangbai333 的回复:
[quote=引用 25 楼 xuzuning 的回复:] 没有什么不好理解的 你只需把 $(id) 当做 document.getElementById(id) 的缩写 就可以了 于是 o = $(id) 后 o 就具有了 id 为 'id' 的 dom 对象的一切属性和方法了
嗯 没错 我就是这么想的,不过上面几位说的要把$()当作一个对象返回,我就不太明白了。因为按照上面几个朋友的代码,返回的$()并没有用到。用到的是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))
[/quote] 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就是你想要有吧
xuzuning 2013-12-01
  • 打赏
  • 举报
回复
没有什么不好理解的 你只需把 $(id) 当做 document.getElementById(id) 的缩写 就可以了 于是 o = $(id) 后 o 就具有了 id 为 'id' 的 dom 对象的一切属性和方法了
jiangbai333 2013-11-30
  • 打赏
  • 举报
回复
引用 20 楼 shengguang1587 的回复:

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);
= -= 像这种 封装方法就可以实现
这个能看懂但是到了匿名函数里就懵了,看网上说要把对象挂载到window对象上,比如$,网上说window.$挂载这个对象,但是$里面又有别的属性,换句话说一级一级属性把我弄得很糊涂,不知道如何把子属性和父属性联系起来。
圣光麦造 2013-11-30
  • 打赏
  • 举报
回复

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);
= -= 像这种 封装方法就可以实现
zhjdg 2013-11-30
  • 打赏
  • 举报
回复
没人接。他不需人接,因为直接用连写了。

<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>
jiangbai333 2013-11-30
  • 打赏
  • 举报
回复
引用 17 楼 l676331991 的回复:
[quote=引用 16 楼 jiangbai333 的回复:] [quote=引用 13 楼 u011461314 的回复:]
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;
		}
}
return new $(id);
把自己作为对象返回该如何理解? 在用$().test()的时候 相当于(new $(id)).test()? 然后又返回一个对象(new (new $(id))).test()? 不太明白[/quote] if在那摆着,哪能让你new两次啊。 伦家$的意思是,你要是调用我的时候,用的是$(id)这种形式,就给你修正一下,论家本来要用new $(id)。不排除某些人很懒,忘记加new。so,给你来了个if,然后给你加上new。如果你就是用的new,那if根本进不去。[/quote] 然后呢? new完的对象返回给谁了? test里面用的是dom属性,根本没用到那个对象阿。
l676331991 2013-11-30
  • 打赏
  • 举报
回复
引用 16 楼 jiangbai333 的回复:
[quote=引用 13 楼 u011461314 的回复:]
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;
		}
}
return new $(id);
把自己作为对象返回该如何理解? 在用$().test()的时候 相当于(new $(id)).test()? 然后又返回一个对象(new (new $(id))).test()? 不太明白[/quote] if在那摆着,哪能让你new两次啊。 伦家$的意思是,你要是调用我的时候,用的是$(id)这种形式,就给你修正一下,论家本来要用new $(id)。不排除某些人很懒,忘记加new。so,给你来了个if,然后给你加上new。如果你就是用的new,那if根本进不去。
jiangbai333 2013-11-30
  • 打赏
  • 举报
回复
引用 13 楼 u011461314 的回复:
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;
		}
}
return new $(id);
把自己作为对象返回该如何理解? 在用$().test()的时候 相当于(new $(id)).test()? 然后又返回一个对象(new (new $(id))).test()? 不太明白
custom1234 2013-11-30
  • 打赏
  • 举报
回复
jiangbai333 2013-11-30
  • 打赏
  • 举报
回复
引用 22 楼 wg0001002 的回复:
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; } }
上面已经有这段代码了
阿布阿土 2013-11-30
  • 打赏
  • 举报
回复
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; } }
zhjdg 2013-11-29
  • 打赏
  • 举报
回复
其实只要会用调试器,理解起来就简单多了。
zhjdg 2013-11-29
  • 打赏
  • 举报
回复
上面是老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;
zhjdg 2013-11-29
  • 打赏
  • 举报
回复
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;
		}
}
ftiger 2013-11-29
  • 打赏
  • 举报
回复
你要干活,就去看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.
天际的海浪 2013-11-29
  • 打赏
  • 举报
回复
引用 10 楼 jiangbai333 的回复:
[quote=引用 9 楼 jslang 的回复:] LZ要先了解下怎么创建对象,那个$()函数就是返回个对象的实例

(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()就不行 ,求解[/quote] 不是说了么。$()函数要返回对象的实例
jiangbai333 2013-11-29
  • 打赏
  • 举报
回复
引用 9 楼 jslang 的回复:
LZ要先了解下怎么创建对象,那个$()函数就是返回个对象的实例

(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()就不行 ,求解
天际的海浪 2013-11-29
  • 打赏
  • 举报
回复
LZ要先了解下怎么创建对象,那个$()函数就是返回个对象的实例
加载更多回复(8)

87,903

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧