2008/12/09

常用的DOM操作、事件

来自:Dustin Diaz,我加上了删除事件。
var Dom = {
get: function(el) {
if (typeof el === 'string') {
return document.getElementById(el);
} else {
return el;
}
},
add: function(el, dest) {
var el = this.get(el);
var dest = this.get(dest);
dest.appendChild(el);
},
remove: function(el) {
var el = this.get(el);
el.parentNode.removeChild(el);
}
};
var Event = {
add: function() {
if (window.addEventListener) {
return function(el, type, fn) {
Dom.get(el).addEventListener(type, fn, false);
};
} else if (window.attachEvent) {
return function(el, type, fn) {
var f = function() {
fn.call(Dom.get(el), window.event);
};
Dom.get(el).attachEvent('on' + type, f);
};
}
}(),
remove:function(){
if (window.removeEventListener) {
return function(el, type,fn) {
Dom.get(el).removeEventListener(type, fn,false);
};
} else if (window.detachEvent) {
return function(el, type, fn) {
Dom.get(el).detachEvent('on' + type);
};
}
}()
};

使用时类似:Event.add('domId', 'click',install);
AS 中的 tweener 语法挺像的:
Tweener.addTween(event.displayObject3D, {
rotationY:180,
time:0.5,
transition:"easeOutQuart"
} );

Labels: , ,