关于事件管理及domReady的完美实现
var K = function () {
var undef;
return {
module : function (newmodule) {
if (this[newmodule] === undef) {
this[newmodule] = {};
return this[newmodule];
}
return this[newmodule];
}
};
}() ;
K.module('util');
K.util.event = (function(){
// http://dean.edwards.name/weblog/2005/10/add-event/
// a counter used to create unique IDs
var guid = 1;
function _addEvent(element, type, handler) {
if (element.addEventListener) {
element.addEventListener(type, handler, false);
} else {
// assign each event handler a unique ID
if (!handler.$$guid) handler.$$guid = guid++;
// create a hash table of event types for the element
if (!element.events) element.events = {};
// create a hash table of event handlers for each element/event pair
var handlers = element.events[type];
if (!handlers) {
handlers = element.events[type] = {};
// store the existing event handler (if there is one)
if (element["on" + type]) {
handlers[0] = element["on" + type];
}
}
// store the event handler in the hash table
handlers[handler.$$guid] = handler;
// assign a global event handler to do all the work
element["on" + type] = handleEvent;
}
}
function _removeEvent(element, type, handler) {
if (element.removeEventListener) {
element.removeEventListener(type, handler, false);
} else {
// delete the event handler from the hash table
if (element.events && element.events[type]) {
delete element.events[type][handler.$$guid];
}
}
}
function handleEvent(event){
var returnValue = true;
// grab the event object (IE uses a global event object)
event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
// get a reference to the hash table of event handlers
var handlers = this.events[event.type];
// execute each event handler
for (var i in handlers) {
this.$$handleEvent = handlers[i];
if (this.$$handleEvent(event) === false) {
returnValue = false;
}
}
return returnValue;
}
function fixEvent(event){
// add W3C standard event methods
event.preventDefault=fixEvent.preventDefault;
event.stopPropagation=fixEvent.stopPropagation;
return event;
}
fixEvent.preventDefault=function(){
this.returnValue=false;
}
fixEvent.stopPropagation=function(){
this.cancelBubble=true;
}
//http://www.thefutureoftheweb.com/blog/adddomloadevent
var load_events = [],load_timer,script,done,exec,
init = function () {
done = true;
clearInterval(load_timer);// kill the timer
// execute each function in the stack in the order they were added
while (exec = load_events.shift()) exec();
if (script) script.onreadystatechange = '';
};
return {
domReady:function(func) {
// if the init function was already ran, just run this function now and stop
if (done) return func();
if (!load_events[0]) {
// for Mozilla/Opera9
if (document.addEventListener) document.addEventListener("DOMContentLoaded", init, false);
// for Internet Explorer
/*@cc_on @*/
/*@if (@_win32)
var proto = "javascript:void(0)";
if (location.protocol == "https:") proto = "//0";
document.write("<script id=__ie_onload defer src="+proto+"><\/scr"+"ipt>");
script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
if (this.readyState == "complete") init();
};
/*@end @*/
// for Safari
if (/WebKit/i.test(navigator.userAgent)) { // sniff
load_timer = setInterval(function() {
if (/loaded|complete/.test(document.readyState)) init(); // call the onload handler
}, 10);
}
// for other browsers set the window.onload, but also execute the old window.onload
_addEvent(window,"load",init);
}
load_events.push(func);
},
addEvent:_addEvent,
removeEvent:_removeEvent
}
})();
K.util.event.domReady(function(){
alert("dom ready!")
})
下载地址:http://kingfishers.googlecode.com/files/K.util.event.js
...全文
相关推荐
读jQuery之十三 添加事件和删除事件的核心方法
最近看完了添加事件和删除事件的核心方法,忍不住想把jQuery的事件模块抠出来。jQuery的代码是非常内聚的,一环套一环。想独立出来不容易。
29Linux基础命令详解——用户管理.docx
29Linux基础命令详解——用户管理
科技服务机构如何通过数据分析增强服务竞争力?.docx
科易网基于40亿+科创知识图谱数据库,深度探索AI技术在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,研究科技创新领域的AI+数智化解决方案,推动科技创新与产业创新智能化发展。
车辆缺陷分割数据集4000张json格式.zip
数据集格式:labelme格式(不包含mask文件,仅仅包含jpg图片和对应的json文件) 图片数量(jpg文件个数):4000 标注数量(json文件个数):4000 标注类别数:1 标注类别名称:["defect"] 每个类别标注的框数: defect count = 8740 总框数:8740 使用标注工具:labelme=5.5.0 所在仓库:firc-dataset 图片分辨率:640x640 标注规则:对类别进行画多边形框polygon 重要说明:可以将数据集用labelme打开编辑,json数据集需自己转成mask或者yolo格式或者coco格式作语义分割或者实例分割 特别声明:本数据集不对训练的模型或者权重文件精度作任何保证
高校如何利用技术转移服务转化科研成果并提升影响力?.docx
高校如何利用技术转移服务转化科研成果并提升影响力?


