豆瓣的jQuery使用技巧
2021年1月13日 | by tgcode
Douban是2.0 社区里面比较成功的一个产品, 里面ajax技术也做得不错, 把它的源码拿来研究了一下, 它在页面上使用了jquery, 我比较喜欢它的一体式的事件处理机制,不用写很多的事件绑定代码,只需要通过一定的命名规则就可以自动给页面元素加上一些功能, 它上面几乎所有的功能都通过这个实现, 配合jquery强大的选择器,代码看起来比较简洁清晰. 下面我们就来看看它的一些核心部分. 我使用的是jquery 1.2.3,压缩之后29kb大小, 速度感觉上比以前有比较大的改善.废话不多说了,直接看看代码吧. 另外推荐一下blueprint 这个css框架,还挺好用的.
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
–>//定义命名空间
varBowtech=newObject();
//注册全局的事件监视器.
Bowtech.EventMonitor=function()
{
this.listeners=newObject();
}
//广播事件
Bowtech.EventMonitor.prototype.broadcast=function(widgetObj,msg,data)
{
varlst=this.listeners[msg];
if(lst!=null)
{
for(varoinlst)
{
lst[o](widgetObj,data);
}
}
}
//绑定所tgcode有的事件.
Bowtech.EventMonitor.prototype.subscribe=function(msg,callback)
{
varlst=this.listeners[msg];
if(lst)
{
lst.push(callback);
}else
{
this.listeners[msg]=[callback];
}
}
//取消事件绑定.
Bowtech.EventMonitor.prototype.unsubscribe=function(msg,callback)
{
varlst=this.listener[msg];
if(lst!=null)
{
lst=lst.filter(function(ele,index,arr)
{returnele!=callback;});
}
}
//Pagescopeevent-monitorobj.
varevent_monitor=newBowtech.EventMonitor();
//对于所有class=”ja_xxxyyy”id=”xxx-123″的元素执行事件绑定,xxx-123部分用来获取元素的ID,比如一个帖子的ID,
//a_xxx后面的部
//分用来标识应用如vote/review/blog等.
//绑定的事件就是:Bowtech.init_vote/Bowtech.init_blog等.
functionload_event_monitor(root)
{
varre=/a_(w+)/;//正则表达式获取ID.
varfns=
{};
$(“.j“,root).each(function(i)
{
varm=re.exec(this.className);
if(m)
{
varf=fns[m[1]];
if(!f)
{//如果事件处理函数不存在则创建函数对象.
f=eval(“Bowtech.init_“+m[1]);
fns[m[1]]=f;//调用绑定函数.
}
f&&f(this);
}
});
}
//在文档加载完毕后将执行的方法(参见jquery文档)
//一般来说文档加载的时候应该绑定所有的事件,但是有一种情况例外.
//比如通过Ajax方法取回来的内容里面还含有动作按钮的,这时需要针对这部分功能执行绑定.
//需要手动调用load_event_monitor(element);方法.
$(function()
{
load_event_monitor(document);
});
//注意这里的o对象是一个html元素而非是一个jquery对象,所以在调用它的方法时应该使用$(o)函数
//把它转化为jquery对象.
Bowtech.init_forder=function(o)
{
vareid=$(o).attr(“id“).split(“–“)[1];
varfo=$(“#f-“+eid);
varunfo=$(“#unf-“+eid);
fo.click(function()
{
$(o).hide();
unfo.show();
fo.hide();
});
unfo.click(function()
{
$(o).show();
fo.show();
unfo.hide();
});
}
jQuery.fn.extend(
{
set_caret:function()
{
if(!$.browser.msie)return;
varinitSetCaret=function()
{this.caretPos=document.selection.createRange().duplicate()};
this.click(initSetCaret).select(initSetCaret).keyup(initSetCaret);
},
insert_caret:function(textFeildValue)
{
vartextObj=this[0];
if(document.all&&textObj.createTextRange&&textObj.caretPos)
{
varcaretPos=textObj.caretPos;
caretPos.text=caretPos.text.charAt(caretPos.text.length–1)==”?textFeildValue+”:textFeildValue;
}elseif(textObj.setSelectionRange)
{
varrangeStart=textObj.selectionStart;
varrangeEnd=textObj.selectionEnd;
vartempStr1=textObj.value.substring(0,rangeStart);
vartempStr2=textObj.value.substring(rangeEnd);
textObj.value=tempStr1+textFeildValue+tempStr2;
textObj.focus();
varlen=textFeildValue.length;
textObj.setSelectionRange(rangeStart+len,rangeStart+len);
textObj.blur();
}else
{
textObj.value+=textFeildValue;
}
}
tgcode})
前台要用就比较简单了, 只需要这样写:
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
–>divid=”test2″class=”mod”>
h3>
这里可以放标题
<!–h3>
divclass=”jmodba_forder”id=”modb-1002″>
这里是一些主要的内容
dl>
dt>Helloworld<!–dt>
dd>
hahaha<!–dtgcoded>
<!–dl>
这个实验在沙加的神舟本上完成
<!–div>
divclass=”edit”>
aid=”f-1002″class=”forder”href=”javascript:void(0);”>[收起]<!–a>aid=”unf-1002″
class=”unforder”href=”javascript:void(0);”>[展开]<!–a>
<!–div>
<!–div>
样式就省略了, 大家可以自己写, 最后发两个效果图:
收起时的样子
相关推荐: Javascript文件加载:LABjs和RequireJS
传统上,加载 Javascript 文件都是使用标签。 就像下面这样: script type=”text/javascript” src=”example.js”>script> 标签很方便,只要加入网页,浏览器就会读取并运行。但是,…