Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.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 为什么我的函数只工作一次(执行document.write()作业)?_Javascript - Fatal编程技术网

Javascript 为什么我的函数只工作一次(执行document.write()作业)?

Javascript 为什么我的函数只工作一次(执行document.write()作业)?,javascript,Javascript,在我的html文件中,我必须在文档的不同位置显示不同股票的图表。我编写了一个javascript函数源(ticker)来显示“ticker”的图表。以下是我的html文档的第一部分: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.

在我的html文件中,我必须在文档的不同位置显示不同股票的图表。我编写了一个javascript函数源(ticker)来显示“ticker”的图表。以下是我的html文档的第一部分:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="CLP_style.css" rel="stylesheet" type="text/css">
<title>The XXXXXX Daily</title>
<script type="text/javascript">
function source(ticker)
{
                var d=new Date();
                var month=new Array(12);
                month[0]="01";
                month[1]="02";
                month[2]="03";
                month[3]="04";
                month[4]="05";
                month[5]="06";
                month[6]="07";
                month[7]="08";
                month[8]="09";
                month[9]="10";
                month[10]="11";
                month[11]="12";
                var startmonth;
                var startyear;
                var endmonth;
                endmonth=month[d.getMonth()];
                switch (endmonth) 
                    {
                    case "01":
                        startmonth="11";
                    case "02":
                        startmonth="12";
                    case "03":
                        startmonth="01";
                    case "04":
                        startmonth="02";
                    case "05":
                        startmonth="03";
                    case "06":
                        startmonth="04";
                    case "07":
                        startmonth="05";
                    case "08":
                        startmonth="06";
                    case "09":
                        startmonth="07";
                    case "10":
                        startmonth="08";
                    case "11":
                        startmonth="09";
                        startyear="2010";
                    case "12":
                        startmonth="10";
                        startyear="2010";
                    }
                var dateparam;
                dateparam=startyear+"-"+startmonth+"-"+d.getDate()+"&e="+d.getFullYear()+"-"+endmonth+"-"+d.getDate();
                var chartsource;
                chartsource="<img src=\"http://xxxxxx.com/chartsymbol_chart.php?s="+dateparam+"&m=line&dateby=1&bollinger=1&bollinger_day=20&sma=1&sma_day=20&chartsize=1&id="+ticker+"\"";
                source=chartsource+" align=\"top\" name=\"HSX\" height=\"250\" width=\"490\">";
                document.write(source);
}
            </script>
</head>

<body>

XXXXXX日报
函数源(ticker)
{
var d=新日期();
var月=新数组(12);
月[0]=“01”;
月[1]=“02”;
月[2]=“03”;
月[3]=“04”;
月[4]=“05”;
月[5]=“06”;
月[6]=“07”;
月[7]=“08”;
月[8]=“09”;
月[9]=“10”;
月[10]=“11”;
月[11]=“12”;
var startmonth;
var startyear;
月底;
endmonth=month[d.getMonth()];
交换机(月末)
{
案例“01”:
startmonth=“11”;
案例“02”:
startmonth=“12”;
案例“03”:
startmonth=“01”;
案例“04”:
startmonth=“02”;
案例“05”:
startmonth=“03”;
案例“06”:
startmonth=“04”;
案例“07”:
startmonth=“05”;
案例“08”:
startmonth=“06”;
案例“09”:
startmonth=“07”;
案例“10”:
startmonth=“08”;
案例“11”:
startmonth=“09”;
startyear=“2010”;
案例“12”:
startmonth=“10”;
startyear=“2010”;
}
var-dateparam;
dateparam=startyear+“-”+startmonth+“-”+d.getDate()+”&e=“+d.getFullYear()+”-“+endmonth+”-“+d.getDate()”;
var图表源;
chartsource=“”;
文件编写(来源);
}
当我调用该函数时,它工作得非常好

<table width="980" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td>
            <script type="text/javascript"> 
                source("^vnindex");
            </script>
        </td>
        <td>
            <script type="text/javascript"> 
                source("^hastc");
            </script>
        </td>
  </tr>
</table>

来源(“^VNIDEX”);
来源(“hastc”);
但只有第一次,即^vnindex图表出现,但^hastc没有出现。起初,我认为这是因为没有股票。但是当我用一个简单的html img标记检查时,^hastc确实出现了。对于文档的其余部分,根本没有显示任何图表。这意味着该函数只工作一次

我不知道如何修复它,如果您能帮我解决这个问题,我将不胜感激

谢谢
Toan Nguyen

请记住该文档。仅当文档正在加载时才写入文档的附件,否则它将尝试用您指定的内容覆盖页面内容!
试试Instean和DOM方法:document.appendChild(createElement(“img”))…

真的应该研究一下循环。谢谢。但我还是不能让它工作。循环只是在一个位置重复创建图像,而我希望它在一个html文档中的不同位置显示。appendChild(createElement(“img”)对我来说有点太复杂了。我只是个初学者。有人能说得更具体一点吗?谢谢