基于Jquery的练习

慢慢接触javascript,javascript是如此的灵活,也越发决得学习javascript很有乐趣.闭包,上下文等等其实真正要学好javascript也不容易,最近学习了一下Jquery下面是一个基于Jquery的练习,希望和大家一起交流。

/**
* @author: xaofeixa
* @date: 2008-04-03
* @purpse: 练习基类
*/
// 命名空间
xaofeixa = window.xaofeixa || {};
(function() {
var BaseObject = function(options) {
//初始化 生成成员的get set方法
BaseObject.prototype.init(this, options);
};
xaofeixa.BaseObject = BaseObject;
BaseObject.prototype = {
action : function(who) {
var propertys = new Array();
for (pro in who) {
if (typeof(who[pro]) != "function") {
propertys[propertys.length] = pro;
}
}
for (i in propertys) {
(function() {
var property = propertys[i];
property = property.charAt(0).toUpperCase()
+ property.substring(1, property.length);
var propertyValue = who[propertys[i]];
who["get" + property] = function() {
return propertyValue;
};
who["set" + property] = function(value) {
propertyValue = value;
};
})();
}
},
init : function(_this, options) {
var who = _this ? _this : this;
if (options) {
/*
* $.each(options, function(optionName, optionValue) {
* _this[optionName] = optionValue; });
*/
$.extend(who, options);
}
this.action(who);
}
};
})();
/**
* @author: xaofeixa
* @date: 2008-04-03
* @purpse: test person
*
*/
(function() {
var Person = function(options) {
Person.prototype.init(this, options);
};
xaofeixa.Person = Person;
$.extend(Person.prototype, xaofeixa.BaseObject.prototype);
})();
/**
* @author: xaofeixa
* @date: 2008-04-03
* @purpse: DOM操作
*/
(function() {
var trim = function(str) {
if (!str) {
str = "";
}
return str;
}
// ///
var Document = function(options) {
Document.prototype.init(this, options);
}
$.extend(Document.prototype, xaofeixa.BaseObject.prototype);
Document.createDocument = function() {
var root = new Element("xml");
return root;
};
xaofeixa.Document = Document;
// //
var Element = function(elementName, text, options) {
this.elementName = elementName;
this.text = text;
this.parentElement = null;
this.sonElements = new Array();
this.attributes = new Array();
Element.prototype.init(this, options);
};
$.extend(Element.prototype, xaofeixa.BaseObject.prototype, {
setElementName : function(elementName) {
this.elementName = elementName;
},
getElementName : function() {
return this.elementName;
},
setText : function(text) {
this.text = text;
},
getText : function() {
return trim(this.text);
},
addElement : function(elementName, text) {
if (!elementName) {
return this;
}
var element = new Element(elementName, text);
element.parentElement = this;
this.sonElements[this.sonElements.length] = element;
return element;
},
removeElement : function() {

},
addAttribute : function(attributeName, attributeValue, options) {
var _options = null;
var _this = this;
if (typeof(attributeName) != "object") {
var attribute = new Attribute(attributeName, attributeValue);
_this.attributes[_this.attributes.length] = attribute;
} else {
_options = attributeName;
}
if (options) {
_options = options;
}
if (_options) {
$.each(_options, function(attributeName, attributeValue) {
var attribute = new Attribute(attributeName, attributeValue);
_this.attributes[_this.attributes.length] = attribute;
});
}
return _this;
},
removeAttribute : function() {

},
getXmlStr : function() {
var xmlStr = "<" + this.getElementName();
// attribute
var attributes = this.attributes;
for (var i = 0; i < attributes.length; i++) {
xmlStr += " " + attributes[i].getAttributeName() + "=\""
+ attributes[i].getAttributeValue() + "\"";
}
xmlStr += ">" + this.getText();
var sonElements = this.sonElements;
for (var i = 0; i < sonElements.length; i++) {
xmlStr += this.getXmlStr.apply(sonElements[i]);
}
xmlStr += "</" + this.getElementName() + ">";
return xmlStr;
},
getSonElement : function(index) {
var _index = index || 0;
var sonCounts = this.sonElements.length;
if (_index >= sonCounts) {
return null;
} else {
return this.sonElements[_index];
}
}
});
xaofeixa.Element = Element;
//
var Attribute = function(attributeName, attributeValue) {
this.attributeName = attributeName;
this.attributeValue = attributeValue;
Attribute.prototype.init(this);
};
$.extend(Attribute.prototype, xaofeixa.BaseObject.prototype);
})();
/**
* @author: xaofeixa
* @date: 2008-04-03
* @purpse: 日志
*
*/
(function() {
var Log = function() {
Log.prototype.init(null, {
logLevel : "INFO,ERROR"
});
return Log.prototype.initLog();
};
$.extend(Log.prototype, xaofeixa.BaseObject.prototype, {
initLog : function() {
var logConsole = new xaofeixa.Element("div");
logConsole.addAttribute({
id : "console",
'class':"console"
});
var aDate = new Date();
var time = aDate.getFullYear() + '-' + aDate.getMonth() + '-'
+ aDate.getDate() + ' ' + aDate.getHours() + ':'
+ aDate.getMinutes();
logConsole.setText("Star Log system time is:" + time);
//logConsole.addAttribute("class","console");
$(logConsole.getXmlStr()).appendTo($('body'));
this.docWidth = document.body.clientWidth;
this.docHeight = document.body.clientHeight;
$("#console").css("top",document.body.clientHeight-$("#console").height());
return this;
},
info : function(message) {
if (this.logLevel.indexOf("INFO") != -1) {
var aDate = new Date();
var time = aDate.getFullYear() + '-' + aDate.getMonth() + '-'
+ aDate.getDate() + ' ' + aDate.getHours() + ':'
+ aDate.getMinutes();
message = time + "[info]: " + message;
$("#console").html($("#console").html() + "<br/>" + message);
}
},
error : function(message) {
if (this.logLevel.indexOf("ERROR") != -1) {
var aDate = new Date();
var time = aDate.getFullYear() + '-' + aDate.getMonth() + '-'
+ aDate.getDate() + ' ' + aDate.getHours() + ':'
+ aDate.getMinutes();
message = time + "[error]: " + message;
$("#console").html($("#console").html() + "<br/>" + message);
}
}
});
xaofeixa.Log = Log;
})();


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值