Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript动态添加html变量_Javascript_Html_Ajax_Variables - Fatal编程技术网

使用javascript动态添加html变量

使用javascript动态添加html变量,javascript,html,ajax,variables,Javascript,Html,Ajax,Variables,我想根据用户选择的数字添加html变量:“var var1=new CalendarPopup();”。目前,我有一个ajax设置,它假定通过更改内部html来更改标记以添加变量,如下所示: <div id="calVar"> <script> var cal1 = new CalendarPopup(); var cal2 = new CalendarPopup(); </script> </div>

我想根据用户选择的数字添加html变量:“
var var1=new CalendarPopup();
”。目前,我有一个ajax设置,它假定通过更改内部html来更改标记以添加变量,如下所示:

<div id="calVar">
    <script>
        var cal1 = new CalendarPopup();
        var cal2 = new CalendarPopup();
    </script>
</div>



        function addRespDropDownAjax(currentNumberOfDropDown)
    {

        //Prepare a new ajaxRequest.
        if (window.XMLHttpRequest) 
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        //Ajax receiving the response in this function
        xmlhttp.onreadystatechange = function() 
        {
            //state 4 is response ready.
            //Status 200 is page found.
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {


                document.getElementById('calVar').innerHTML = '<script>var cal3 = new CalendarPopup();</script>';
                    alert(document.getElementById('calVar').innerHTML);

            }
        };

        //Send the Ajax request.
        xmlhttp.open('GET','mainServlet?command=ajax.AddRespDropDown&NUMBER_OF_DROP_DOWN=' + currentNumberOfDropDown, true);
        xmlhttp.send();

    }

var cal1=新日历弹出窗口();
var cal2=新日历弹出窗口();
函数addRespDropDownAjax(currentNumberOfDropDown)
{
//准备一个新的请求。
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}else{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
//Ajax在此函数中接收响应
xmlhttp.onreadystatechange=函数()
{
//状态4是响应就绪。
//已找到状态为200的页面。
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById('calVar').innerHTML='var cal3=new CalendarPopup();';
警报(document.getElementById('calVar').innerHTML);
}
};
//发送Ajax请求。
open('GET','mainServlet?command=ajax.AddRespDropDown&NUMBER_OF_DROP_DOWN='+currentNumberOfDropDown,true);
xmlhttp.send();
}
最后一个警报:document.getElementById('calVar')。innerHTML不返回任何内容,并且未创建我的变量。有什么想法吗


非常感谢

HTML没有变量,任何在
中定义的没有更深嵌套范围的变量都可以在
窗口
对象外简单定义

不要尝试插入HTML,只需使用:

window.cal3 = new CalendarPopup();

然后任何其他脚本现在或以后都可以访问此变量。

这是一个JavaScript变量,而不是HTML变量。为什么不在脚本中创建JS呢?你真的想得太多了。
window.cal3=newCalendarPopup()。完成了,你收到200的回复了吗?你试过调试(断点)吗?addRespDropDownAjax是在div之后调用的吗?为什么不直接分配cal3(正如前面的评论所指出的)。@Friso我真的无法调试它。我正在使用eclipse,无法安装任何其他程序,因为它不是我的计算机。