JS 原型应用

<html>

<body>

<inputtype="text"name="txtName"value="123"id="txtName">

<inputtype="button"name="btn"id="btn"value="confirm"></input>

<script>

(function(window){

varjQuery=function(id){

returnjQuery.fn.init(id);

}

jQuery.fn=jQuery.prototype;

jQuery.fn={

init:function(id){

this.id=id;

this.element=document.getElementById(this.id);

returnthis;

},

html:function(html){

this.element.innerHTML=html;

returnthis;

}

};

jQuery.fn.init.prototype=jQuery.fn;

jQuery.fn.val=function(){

returnthis.element.value;

}

window.$=window.jQuery=jQuery;

})(window);

(function($){

$.fn.click=function(callback){

this.element.onclick=callback;

returnthis;

}

$.trim=function(str){

returnstr.replace(/(^\s*)|(\s*$)/g,"");

}

})(jQuery)

$("btn").click(function(){

alert($("txtName").val())

})

alert("11"+$.trim("fdsafdsa"));

</script>

</body>

</html>

相关推荐