js的面向对象编程

时间: 2009-11-12 / 分类: JavaScript / 浏览次数: / 0个评论 发表评论

js的面向对象在我心里一直是个阴影,之前写的代码都没用到,终于敢尝试写个例子:

<script type=”text/javascript”>
var mail=function(){
this.xmlHttp=new XMLHttpRequest();
this.show=document.getElementById(“show”);
}
mail.prototype={
ajaxload:function(type,url,data,func){
this.xmlHttp.onreadystatechange=func;
this.xmlHttp.open(type,url,true);
this.xmlHttp.send(data);
}
}

//调用
function login(){
var user=document.getElementById(“user”).value;
var pass=document.getElementById(“pass”).value;
var url=”http://*****.net/api/login.ashx?u=”+user+”&p=”+pass;
var t=”get”;
var m=new mail();
m.ajaxload(t,url,”null”,function(){
if(m.xmlHttp.readyState==1){
m.show.innerHTML=”加载中…”;
}
if(m.xmlHttp.readyState==4){
m.show.innerHTML=m.xmlHttp.responseText;
}
})
}
</script>

html:

<body>
U<input type=”text” id=”user”/><br/>
P<input type=”text” id=”pass”/><br/>
G<input type=”button” onclick=”login()” value=”提交”/>
<div id=”show”></div>
</body>

发表评论

您的昵称 *

您的邮箱 *

您的网站