日期:2014-05-16  浏览次数:20683 次

新手正在学习,问一个ajax问题.求解答.
我创建了一个XHR对象以后.如果一个页面有很多需要ajax交互的地方,怎么实现呀.比如,如下.
JScript code

_createXMLHttpRequest = function() {
    var xhr = null;  
    if(typeof XMLHttpRequest != 'undefined') {  
        xhr = new XMLHttpRequest();  
    }else{  
        var _msxmlhttp = new Array(  
            'Msxml2.XMLHTTP.6.0',  
            'Msxml2.XMLHTTP.3.0',  
            'Msxml2.XMLHTTP',  
            'Microsoft.XMLHTTP');  
        for(var i = 0; i < _msxmlhttp.length; i++) {  
            try {  
                if (xhr = new ActiveXObject(_msxmlhttp[i])) break;  
            } catch (e) {
                xhr = null;
            }  
        }  
    }  
    if(!xhr)
    {
        alert("Could not create connection object.");    
    } 
    return xhr;
};  

HTML code

<button type="button" id="bt1"></button>
<button type="button" id="bt2"></button>

<div id="bt3">这里按了bt1之后改变内容的地方</div>
<duv id="bt4">这里按了bt2之后改变内容的地方</div>



求解答啊,本人正在学习.希望稍微详细一点.谢谢.

------解决方案--------------------
<button type="button" id="bt1" onclick="x('bt3')"></button>
<button type="button" id="bt1" onclick="x('bt4')"></button>.

function x(btn)
{
http = createXMLHttpRequest()
http.open("get",url,true)
http.onreadystatechange = function()
{
 if(http.status==200 && http.readyState==4)
{
 document.getElementById(btn).innerHTML=http.responseText
}
};
http.send();
}
------解决方案--------------------
确实是要创建多次的。内容不一样,那你就传参吧。