<title>构造函数</title><script>$ = document.getElementById;function news() {this.divid = "";this.get = function(divid) {xmlHttp = this.GetXmlHttpObject();if (xmlHttp==null) {alert ("您的浏览器不支持AJAX!");return;}this.divid = divid;//this.get_Changed();var url = "news.php";xmlHttp.open("GET", url, true);xmlHttp.onreadystatechange=this.get_Changed;xmlHttp.send(null);},this.get_Changed = function() {if (xmlHttp.readyState==4) {str = xmlHttp.responseText;$(this.divid).innerHTML = str;alert(this.divid) //为什么this.divid 为undefine呢}}this.GetXmlHttpObject = function() {var xmlHttp=null;try { // Firefox, Opera 8.0+, SafarixmlHttp=new XMLHttpRequest();}catch (e) { // Internet Explorertry {xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch (e) {xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}}return xmlHttp;}}var News = new news();News.get("aa"); //在aa中加载news.php的内容</script></head><body><div id="aa"></div></body>已经解决了,确实是作用域的问题,谢谢抛砖引玉用_this就搞定了function news() {var _this = this;this.divid = "";this.get = function(divid) {……xmlHttp.open("GET", url, true);xmlHttp.onreadystatechange=this.get_Changed;xmlHttp.send(null);},this.get_Changed = function() {if (xmlHttp.readyState==4) {str = xmlHttp.responseText;$(_this.divid).innerHTML = str;alert(_this.divid);}}}
添加回答
举报
0/150
提交
取消