您好,欢迎来到爱够旅游网。
搜索
您的当前位置:首页Base层代码

Base层代码

来源:爱够旅游网
JS中属于框架级的base层代码
  /*
  *  base 层代码
  */
  
   var GLOBAL = {};
   GLOBAL.namespace = function(str){
     var arr=str.split("."),  o=GLOBAL;
	 for(i=(arr[0]=="GLOBAL") ? 1:0; i<arr.length; i++) {
	 o[arr[i]]=o[arr[i]] || {};
	 o=o[arr[i]];
	 }
	 }
   
    //DOM相关
	GLOBAL.namespace("Dom");
	GLOBAL.Dom.getNextNode=function(node){
	node=typeof node=="string" ? document.getElementById(node) : node;
	var nextNode = node.nextSibling;
	if(!nextNode) return null;
	if(!document.all){
	while(true){
	if(nextNode.nodeType == 1){
	break;
	}else {
	if(nextNode.nextSibling){
	nextNode=nextNode.nextSibling;
	}else{
     break;
	}
	}
	}
	}
	return nextNode;
	};
	
	GLOBAL.Dom.setOpacity = function(node,level){
	node = typeof node=="string" ? document.getElementById(node) : node;
    if(document.all){
     node.style.filter = 'alpha(opacity=' + level +')';
     }	else {
	 node.style.opacity = level/100;
	 }
	}
   
    GLOBAL.Dom.get= function(node){
    node = typeof node=="string" ? document.getElementById(node) : node;
    return node; 
	}
	
	GLOBAL.Dom.getElementByClassName = function (str, root,tag) {
	if(root) {
	 root = typeof root =="string" ? document.getElementById(root) : root;
	} else {
	root =document.body;
	}
	tag= tag || "*";
	var els = root.getElementTagName(tag),  arr=[];
	for(var i=0; n=els.length; i<n; i++){
	for(var j=0, k=els[i].className.split(" "),l=k.length; j<l; j++){
	if(k[j] == str){
	arr.push(els[i]);
	break;
	}
	}
	}
	return arr;
	}
	
	//Event相关
	GLOBAL.namespace("Event");
	GLOBAL.Event.getEventTarget= function(e){
	e= window.event || e;
	return e.srcElement || e.target;
	}else {
	  e.stopPropagation();
	}
   }
    GLOBAL.Event.on= function(node,eventType,handler){
	node = typeof node=="string" ? document.getElementById(node) : node;
    if(document.all){
	node.attachEvent("on"+eventType,handler);
	}else{
	node.addEventListener(eventType,handler,false);
	}
	}
	
	//Lang相关
    GLOBAL.namespace("Lang");
	GLOBAL.Lang.trim = function(ostr){
	retrun ostr.replace(/^\s+|\s=$/g,"");
	}
	GLOBAL.Lang.isNumber=function(s){
	return !isNan(s);
	}
	GLOBAL.Lang.extend = function(subClass,superClass){
	var F=function(){};
	F.prototype= superClass.prototype;
	subClass.prototype=new F();
	subClass.prototype.constructor=subClass;
	subClass.superClass=superClass.prototype;
	if(superClass.prototype.constructor==Object.prototype.constructor){
	superClass.prototype.constructor=superClass;
	}
	}

在实际应用中,我们可以将base层的代码放在一个JS文件中,如base.js,然后通过<script src="base.js"></script>链接到网页中。如果对代码的大小要求较高,还可以将

base.js分成粒度更小的base_dom.js,base_event.js和base_lang.js,按需导入。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- igbc.cn 版权所有 湘ICP备2023023988号-5

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务