我们在编写JS源码时,很多时候都要用到document.getElementById()
来获得对象,每次都这么写显得很麻烦,于是就在网上找了一种用$定义方式
源码
function $(objectId) {
if(document.getElementById && document.getElementById(objectId)) {
// W3C DOM
return document.getElementById(objectId);
}else if (document.all && document.all(objectId)) {
// MSIE 4 DOM
return document.all(objectId);
}else {
return false;
}
}
我们还可以通过一个函数来定义
比如
var getById = function(id){
return document.getElementById(id);
}
使用时可以通过getById(id)来调用即可