手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表2009年07月28日的文章

javascript处理事件的一些兼容写法

 

司徒正美的一些关于javascript的处理的兼容写法。如果不想用这些,可以直接用jQuery中的功能。如bind,unbind,$('XX'),$(document).ready()等

绑定事件

JavaScript代码
  1. var addEvent = function( obj, type, fn ) {  
  2.     if (obj.addEventListener)  
  3.         obj.addEventListener( type, fn, false );  
  4.     else if (obj.attachEvent) {  
  5.         obj["e"+type+fn] = fn;  
  6.         obj.attachEvent( "on"+type, function() {  
  7.             obj["e"+type+fn]();  
  8.         } );  
  9.     }  
  10. };  

另一个实现

JavaScript代码
  1. var addEvent = (function () {   
  2.     if (document.addEventListener) {   
  3.         return function (el, type, fn) {   
  4.             el.addEventListener(type, fn, false);   
  5.         };   
  6.     } else {   
  7.         return function (el, type, fn) {   
  8.             el.attachEvent('on' + type, function () {   
  9.                 return fn.call(el, window.event);   
  10.             });   
  11.         }   
  12.     }   
  13. })();  

移除事件

JavaScript代码
  1. var removeEvent = function(obj, type, fn) {  
  2.     if (obj.removeEventListener)  
  3.         obj.removeEventListener( type, fn, false );  
  4.     else if (obj.detachEvent) {  
  5.         obj.detachEvent( "on"+type, obj["e"+type+fn] );  
  6.         obj["e"+type+fn] = null;  
  7.     }  
  8. }  

加载事件与脚本

JavaScript代码
  1. var loadEvent = function(func) {  
  2.     var oldonload = window.onload;  
  3.     if (typeof window.onload != 'function') {  
  4.         window.onload = func;  
  5.     }else {  
  6.         window.onload = function() {  
  7.             oldonload();  
  8.             func();  
  9.         }  
  10.     }  
  11. }  

阻止事件

JavaScript代码
  1. var cancelEvent = function(event) {  
  2.     event = event||window.event  
  3.     if (event.preventDefault) {  
  4.         event.preventDefault(  );  
  5.         event.stopPropagation(  );  
  6.     } else {  
  7.         event.returnValue = false;  
  8.         event.cancelBubble = true;  
  9.     }  
  10. }  

取得事件源对象

相当于Prototype.js框架的Event.element(e)

JavaScript代码
  1. var getTarget = function(event){  
  2.     event = event || window.event;  
  3.     var obj = event.srcElement ? event.srcElement : event.target;  
  4.     return obj  
  5. }  

删除广告

本期阿里妈妈的广告结束后,开始清理广告。
同时征租广告位:
1、logo旁
2、全站左侧
3、文章内页
4、友情链接(目前清理中)


说明:

想知道IP、PV之类的请看网站底部的统计代码(已开放浏览功能)。

Tags: 广告

What makes a good programmer?

本文来自nio's blog(nio?尼奥?黑客帝国?),原文如下:

What makes a good programmer?

Some casual surfing led me to this article from a couple of years ago, titled "How to recognize a good programmer". It was a nice read, but as many in the comments pointed out, the criteria the author set forth most likely describe himself and are not really useful as rules-of-thumb on how to recognize a good programmer.

It got me thinking though, on what are the attributes I consider useful in fellow programmers. So what makes a good programmer?

以下五项,按照优先级,你会怎样排序呢?

  • Security(安全性)
  • Maintainability(可维护性)
  • Usability(可用性)
  • Performance(性能)
  • LOC (lines-of-code) count(代码量)

作者认为最重要的是 usability,因为你开发的东西最终价值取决于最终用户。我们开发的目的是为了解决问题,如果解决不了问题,则说明项目是失败的。