日期:2014-05-16 浏览次数:20410 次
<html>
<head>
<script language="javascript" defer="defer">
// 定义Longfei为框架名称,将Longfei定义为window对象的属性
var Longfei = window.Longfei = window.$ = function(selector){return new Longfei.fn.init(selector)};
Longfei.fn = Longfei.prototype ={
init:function(selector){
this.selector=selector;
return this;
},
val:function(){
//在此可以通过对selector进行判断,如果以#开头,则通过id获取对象,如果以.开头,则通过class获取对象等等
if(this.selector.substring(0,1)=="#"){
var vId = this.selector.substring(1);
var obj = document.getElementById(vId);
return obj.value;
}else if(this.selector.substring(0,1)=="."){
var vArray = new Array();
var vClassName = this.selector.substring(1);
var obj = document.getElementsByTagName("*");
for(var i in obj){
if(obj[i].className == vClassName){
vArray.push(obj[i]);
}
}
//在这里可以对vArray迭代取值。
return vArray;
}
}
Longfei.fn.init.prototype = Longfei.fn;
var babyLongf = new Longfei("#myInput");
alert(babyLongf.val());//Hello world!
</script>
</head>
<body>
<form method="post" action="" name="myform">
<input id="myInput" type="text" value="Hello world!" size="50" />
</form>
</body>
</html>