//定义命名空间
var Bowtech=new Object();
![](http://img.warting.com/allimg/2013/c1124/13U2245N96230-132W.jpg)
//注册全局的事件监视器.
![](http://img.warting.com/allimg/2013/c1124/13U2245O41b0-22111.jpg)
Bowtech.EventMonitor = function()
{
this.listeners = new Object();
}
//广播事件
![](http://img.warting.com/allimg/2013/c1124/13U2245O41b0-22111.jpg)
Bowtech.EventMonitor.prototype.broadcast=function(widgetObj, msg, data)
{
var lst = this.listeners[msg];
![](http://img.warting.com/allimg/2013/c1124/13U2245P3LZ-59143.jpg)
![](http://img.warting.com/allimg/2013/c1124/13U2245Pc250-J406.jpg)
if(lst != null)
{
![](http://img.warting.com/allimg/2013/c1124/13U2245Pc250-J406.jpg)
for(var o in lst)
{
lst[o](widgetObj, data);
}
}
}
//绑定所有的事件.
![](http://img.warting.com/allimg/2013/c1124/13U2245O41b0-22111.jpg)
Bowtech.EventMonitor.prototype.subscribe=function(msg, callback)
{
var lst = this.listeners[msg];
![](http://img.warting.com/allimg/2013/c1124/13U2245Pc250-J406.jpg)
if (lst)
{
lst.push(callback);
![](http://img.warting.com/allimg/2013/c1124/13U2245Pc250-J406.jpg)
} else
{
this.listeners[msg] = [callback];
}
}
//取消事件绑定.
![](http://img.warting.com/allimg/2013/c1124/13U2245O41b0-22111.jpg)
Bowtech.EventMonitor.prototype.unsubscribe=function(msg, callback)
{
var lst = this.listener[msg];
![](http://img.warting.com/allimg/2013/c1124/13U2245Pc250-J406.jpg)
if (lst != null)
{
![](http://img.warting.com/allimg/2013/c1124/13U2245Pc250-J406.jpg)
lst = lst.filter(function(ele, index, arr)
{return ele!=callback;});
}
}
![](http://img.warting.com/allimg/2013/c1124/13U2245N96230-132W.jpg)
// Page scope event-monitor obj.
var event_monitor = new Bowtech.EventMonitor();
//对于所有 class="j a_xxx yyy" id="xxx-123"的元素执行事件绑定, xxx-123部分用来获取元素的ID,比如一个帖子的ID,
// a_xxx 后面的部
//分用来标识应用如 vote / review / blog 等.
//绑定的事件就是 : Bowtech.init_vote / Bowtech.init_blog 等.
![](http://img.warting.com/allimg/2013/c1124/13U2245O41b0-22111.jpg)
function load_event_monitor(root)
{
var re = /a_(w+)/; //正则表达式获取ID.
![](http://img.warting.com/allimg/2013/c1124/13U2245Pc250-J406.jpg)
var fns =
{};
![](http://img.warting.com/allimg/2013/c1124/13U2245Pc250-J406.jpg)
$(".j", root).each(function(i)
{
var m = re.exec(this.className);
![](http://img.warting.com/allimg/2013/c1124/13U2245Pc250-J406.jpg)
if (m)
{
var f = fns[m[1]];
![](http://img.warting.com/allimg/2013/c1124/13U2245Pc250-J406.jpg)
if (!f)
{ //如果事件处理函数不存在则创建函数对象.
f = eval("Bowtech.init_"+m[1]);
fns[m[1]] = f;//调用绑定函数.
}
f && f(this);
}
});
}
//在文档加载完毕后将执行的方法(参见jquery文档)
//一般来说文档加载的时候应该绑定所有的事件, 但是有一种情况例外.
//比如 通过Ajax方法取回来的内容里面还含有动作按钮的,这时需要针对这部分功能执行绑定.
//需要手动调用 load_event_monitor(element); 方法.
![](http://img.warting.com/allimg/2013/c1124/13U2245O41b0-22111.jpg)
$(function()
{
load_event_monitor(document);
});
//注意这里的o对象是一个html 元素而非是一个jquery对象,所以在调用它的方法时应该使用$(o)函数
//把它转化为jquery对象.
![](http://img.warting.com/allimg/2013/c1124/13U2245O41b0-22111.jpg)
Bowtech.init_forder = function(o)
{
var eid = $(o).attr("id").split("-")[1];
var fo = $("#f-"+eid);
var unfo = $("#unf-"+eid);
![](http://img.warting.com/allimg/2013/c1124/13U2245Pc250-J406.jpg)
fo.click(function()
{
$(o).hide();
unfo.show();
fo.hide();
});
![](http://img.warting.com/allimg/2013/c1124/13U2245Pc250-J406.jpg)
unfo.click(function()
{
$(o).show();
fo.show();
unfo.hide();
});
}
![](http://img.warting.com/allimg/2013/c1124/13U2245N96230-132W.jpg)
![](http://img.warting.com/allimg/2013/c1124/13U2245N96230-132W.jpg)
![](http://img.warting.com/allimg/2013/c1124/13U2245O41b0-22111.jpg)
jQuery.fn.extend(
{
![](http://img.warting.com/allimg/2013/c1124/13U2245Pc250-J406.jpg)
set_caret: function()
{
if(!$.browser.msie) return;
![](http://img.warting.com/allimg/2013/c1124/13U2245Pc250-J406.jpg)
var initSetCaret = function()
{this.caretPos = document.selection.createRange().duplicate()};
this.click(initSetCaret).select(initSetCaret).keyup(initSetCaret);
},
![](http://img.warting.com/allimg/2013/c1124/13U2245Pc250-J406.jpg)
insert_caret:function(textFeildValue)
{
var textObj = this[0];
![](http://img.warting.com/allimg/2013/c1124/13U2245Pc250-J406.jpg)
if(document.all && textObj.createTextRange && textObj.caretPos)
{
var caretPos=textObj.caretPos;
caretPos.text = caretPos.text.charAt(caretPos.text.length-1) == '' ? textFeildValue+'' : textFeildValue;
![](http://img.warting.com/allimg/2013/c1124/13U2245Pc250-J406.jpg)
} else if(textObj.setSelectionRange)
{
var rangeStart=textObj.selectionStart;
var rangeEnd=textObj.selectionEnd;
var tempStr1=textObj.value.substring(0,rangeStart);
var tempStr2=textObj.value.substring(rangeEnd);
textObj.value=tempStr1+textFeildValue+tempStr2;
textObj.focus();
var len=textFeildValue.length;
textObj.setSelectionRange(rangeStart+len,rangeStart+len);
textObj.blur();
![](http://img.warting.com/allimg/2013/c1124/13U2245Pc250-J406.jpg)
} else
{
textObj.value+=textFeildValue;
}
}
})
前台要用就比较简单了, 只需要这样写:
div id="test2" class="mod"
h3
这里可以放标题
/h3
div class="j modb a_forder" id="modb-1002"
这里是一些主要的内容
dl
dtHello world/dt
dd
hahaha/dd
/dl
这个实验在沙加的神舟本上完成
/div
div class="edit"
a id="f-1002" class="forder" href="javascript:void(0);"[收起]/a a id="unf-1002"
class="unforder" href="javascript:void(0);"[展开]/a
/div
/div
样式就省略了, 大家可以自己写, 最后发两个效果图:
![](http://img.warting.com/allimg/2013/c1124/13U2245R0P60-102041.jpg)
收起时的样子
![](http://img.warting.com/allimg/2013/c1124/13U2245R5Y0-113261.jpg)
开发环境: 沙加的神舟本, VS2008, Framework 2.0