$("div").text()html
$("div")text("1111")bash
$("input").value()ui
//在原型上继续添加
Liang.prototype = {
constructor : Liang,
init : function(){}, //(1)
html : function(){}, //(2)
on : function(){}, //(3)
off : function(){}, //(3)
//获取或者设置元素的innerText
text : function(str){
if(typeof str === "undefined"){ // 例如$("div").text()
//表示获取第0个 innerText
return this[0].innerText;
}else if(typeof str === "string"){ // 例如 $("div").text("1111")
//设置内容
Liang.each(this, function(v){
v.innerText = str;
});
return this;
}
},
//获取元素的value值(表单元素才有)
val : function(str){
if(typeof str === "undefined"){ // 例如$("input").value()
//获取第0个value值
return this[0].value;
}else{
Liang.each(this, function(v){
v.value = str;
});
return this;
}
},
//eq
eq : function(n){
var len = this.length;
if(n >= len || n < 0){
throw Error("下标越界,检查eq内的参数");
}
return new this.init(this[n]);
}
}
复制代码