我想根据用户选择的数字添加html变量:“<script>var var1 = new CalendarPopup();</script>
”。目前,我有一个想改变我的标记通过改变内部HTML像这样添加变量的AJAX设置:用javascript动态添加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();
}
最后警告:的document.getElementById(“calVar”)的innerHTML任何回报而我的变化不会被创造出来。有任何想法吗?
非常感谢!
这是一个JavaScript变量,而不是HTML变量。为什么不在脚本中创建JS? – zzzzBov 2013-02-20 14:52:46
你真的超越了这一点。 – JJJ 2013-02-20 14:53:22
'window.cal3 = new CalendarPopup();'。完成。 – 2013-02-20 14:55:04