求高手解惑

mengnanleo 2011-06-16 04:14:13

有没有人知道,类似juery的$('#id').a().b().b()
这种结构是怎么实现的呀
小弟只能做到$('#id').a()就不能继续
那种写法的原理是什么呀,能写点简单的小例子解答下么
...全文
153 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
mengnanleo 2011-06-16
  • 打赏
  • 举报
回复
简单写法

(function(){
var MN=function(ele){
if(!ele) return false;
ele=ele.split(/\s/);
var $=selector(ele);
function selector(ele){
return (ele.length==2)?document.getElementById(ele[0]).getElementsByTagName(ele[1]):document.getElementById(ele[0]);
}
return{
getOffset:function(){
var top=0,left=0;
while($!= null){
top+=$.offsetTop;
left+=$.offsetLeft;
$=$.offsetParent;
}
return {top:top,left:left};
},
opacity:function(v){
if(typeof v!='number') v=100;
if(!-[1,]) $.style.filter='alpha(opacity='+v+')';
$.style.MozOpacity=v/100;
$.style.opacity=v/100;
return this
},
html:function(v){
if(!v) return $.innerHTML;
$.innerHTML=v;
return this;
},
eq:function(v){
if(typeof v=='number') $=$[v];
return this;
}
}
}
window.$=MN;
})()


正常写法

(function(){
var MN=function(selector){
return new MN.fn.init(selector);
}
MN.fn=MN.prototype={
init:function(selector){
if(selector){
selector=selector.split(' ');
this.$=(selector.length==2)?document.getElementById(selector[0]).getElementsByTagName(selector[1]):document.getElementById(selector[0]);
}
return this;
},
eq:function(v){
if(typeof v=='number') this.$=this.$[v];
return this;
},
getOffset:function(){
var top=0,left=0;
while(this.$!= null){
top+=this.$.offsetTop;
left+=this.$.offsetLeft;
this.$=this.$.offsetParent;
}
return {top:top,left:left};
},
html:function(v){
if(!v) return this.$.innerHTML;
this.$.innerHTML=v;
return this;
},
opacity:function(v){
if(typeof v!='number') v=100;
if(!-[1,]) this.$.style.filter='alpha(opacity='+v+')';
this.$.style.MozOpacity=v/100;
this.$.style.opacity=v/100;
return this;
},
extend:function(obj){
for(var i in obj){
this[i]=obj[i];
}
}
}
MN.fn.init.prototype=MN.fn;
window.$=MN;
})()


第一种是不是不能使用extend,想不到怎么满足

而且貌似第一种不太灵活,函数不能相互调用
mengnanleo 2011-06-16
  • 打赏
  • 举报
回复
汗了,大哥,我是自己在写呀,没有用jquery
jhrxx 2011-06-16
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 mengnanleo 的回复:]
如果修改了$('#id')的话end()就派上作用了

怎么说呀
[/Quote]
返回值:jQuery
end()
概述
回到最近的一个"破坏性"操作之前。即,将匹配的元素列表变为前一次的状态。

如果之前没有破坏性操作,则返回一个空集。所谓的"破坏性"就是指任何改变所匹配的jQuery元素的操作。这包括在 Traversing 中任何返回一个jQuery对象的函数--'add', 'andSelf', 'children', 'filter', 'find', 'map', 'next', 'nextAll', 'not', 'parent', 'parents', 'prev', 'prevAll', 'siblings' and 'slice'--再加上 Manipulation 中的 'clone'。

示例
描述:
选取所有的p元素,查找并选取span子元素,然后再回过来选取p元素

HTML 代码:
<p><span>Hello</span>,how are you?</p>jQuery 代码:
$("p").find("span").end()结果:
[ <p><span>Hello</span> how are you?</p> ]
看看文档吧
例子
$("p").find("span").end().a().b().b()
a()和b()还是对$("p")操作,而不是$("p span")
mengnanleo 2011-06-16
  • 打赏
  • 举报
回复
如果修改了$('#id')的话end()就派上作用了

怎么说呀
jhrxx 2011-06-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 mengnanleo 的回复:]
功能是实现了,可是还是有点不明白哟。。

多谢二楼,这么快的解答了
[/Quote]

谢了我2回。。。
还有什么不明白的
mengnanleo 2011-06-16
  • 打赏
  • 举报
回复
是1楼。。。打错了。。。

也谢谢2楼得大哥捧场
mengnanleo 2011-06-16
  • 打赏
  • 举报
回复
功能是实现了,可是还是有点不明白哟。。

多谢二楼,这么快的解答了
mengnanleo 2011-06-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 chrisak 的回复:]

原理就是a b 不管干了啥最终都是执行
return this;
[/Quote]
能说详细点么~~~
jhrxx 2011-06-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 chrisak 的回复:]
原理就是a b 不管干了啥最终都是执行
return this;
[/Quote]
顶,这就相当于把$('#id')当做返回值返回了,如果修改了$('#id')的话end()就派上作用了
ChrisAK 2011-06-16
  • 打赏
  • 举报
回复
原理就是a b 不管干了啥最终都是执行
return this;

87,989

社区成员

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

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