无法加载JavaScript代码

时间:2016-08-23 03:29:11

标签: javascript html ajax jsp

我在进行ajax调用后使用java脚本在div中显示带有新JS内容的数据。请参考以下代码:

//来自a.jsp的ajax调用

var goUrl = "/testMethod/getTestMethod;
            var httpRequest=null;
            var refreshContent = "null";
            httpRequest = XMLHTTPObject();
            httpRequest.open("POST", goUrl, true);
            httpRequest.onreadystatechange = function () {ajaxFunction(refreshThisDiv,httpRequest); } ;
            httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            httpRequest.send(null);



function ajaxFunction(refreshThisDiv,httpRequest){

    var serversideValidation = true;
    if (httpRequest.readyState == 4)
    {
        if(httpRequest.status == 200)
        {
            results =  httpRequest.responseText;  // http.responseXML; which will lead to an XML based response, if we were to have some XML output from a server file

            if(results != 'null') {
                var test= document.getElementById(refreshThisdiv);

                test.style.display = '' ;
                test.innerHTML =  results;


            }

//下面是b.jsp,这是要显示的新内容。

<div id="test">
</div>
<script>
 var test = document.getElementById("test");
test.innerHTML ="HI";
</script>

结果很好并且重定向到b.jsp并显示html内容。但标签不起作用:(

我希望在完成该div的ajax调用后看到你好。请帮帮我。

2 个答案:

答案 0 :(得分:4)

ID不是#test,而是test#test是您与jQuery,CSS或document.querySelector一起使用的选择器。不出所料,document.getElementById需要身份证。 :)

答案 1 :(得分:2)

getElementById()只需要ID的名称。您通过传递#test只需要test的地方错误地使用了CSS-ish语法。

更正了新代码:

<div id="test">
</div>
<script>
  var test = document.getElementById("test");
  test.innerHTML ="HI";
</script>