-1

我只是想读取一个 div 中的 html 文件,现在它不起作用,因此我尝试读取一个名为 a.txt 的简单文本文件,该文本文件包含 3 行“asdasdas”之类的东西。

它只是不起作用,按下段落标签后正在调用该函数,这是代码:

function New()
{       
    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("divfull").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","a.txt",true);
    xmlhttp.send(null);
}
4

1 回答 1

0

Where exactly do you get the syntax error?

Try using jQuery:

$.ajax({
    type: 'get'
    url: '', //LOCATION OF TEXT FILE
    success: function(data) {
        $('#divfull').html(data);
    }
});

Since its jQuery it will take care of the browser type (so you won't have to create an ActiveXObject for older IE versions). Plus, its fewer lines of code.

于 2012-08-07T01:24:32.740 回答