我编写了一个javascript函数来获取一个数组作为参数,但是当我想访问数组的条目时总是会发生一个未定义的错误。
function putStringTwo(k){
var table=document.getElementById("st2");
var tbody=document.createElement("tbody");
tbody.setAttribute("id","2td2");
var sCell=new Array(12);
var sRow=new Array(12);
for(var i=0;i<12;i++){
sCell[i]=document.createElement("td");
sCell[i].setAttribute("name",k[i]); //error happens here
sCell[i].innerHTML=k[i];
sCell[i].setAttribute("id","two"+i);
}
for(var i=0;i<12;i++){
sRow[i]=document.createElement("tr");
sRow[i].appendChild(sCell[i]);
tbody.appendChild(sRow[i]);
}
table.appendChild(tbody);
for(var i=0;i<12;i++)
$('#two'+i).addClass("drag");
}
这是来电者:
//here is the part of php code used for generate a javascript function
<?php
$counter=0;
$flag=array();
$numdatabasekey=12;
for($counter=1;$counter<=$numdatabasekey;$counter++)
$flag[$counter]=0;
$counter=0;
while($counter<12){
$number=rand(1,$numdatabasekey);
if($flag[$number]==0){
$keynum[$counter]=$number;
$flag[$number]=1;
$counter++;
}
}
$keyword=array();
if(!($database=mysql_connect("mysql.comp.polyu.edu.hk","xxxxxxxxxxx","xxxxxxxxxx")))
die("Unable to connect to database </body></html>");
if(!mysql_select_db("10821473d",$database))
die("unable to open 10821473d database </body></html>");
for($counter=0;$counter<12;$counter++){
$query="SELECT * FROM Keyword WHERE id=$keynum[$counter]";
if(!($result=mysql_query($query,$database))){
print("could not execute query <br/>");
die(mysql_error()."</body></html>");
}
$row=mysql_fetch_row($result);
$keyword[$counter]=$row[1];
}
mysql_close($database);
$javascript=<<<JS
function putImageTwo(){
var table=document.getElementById("tb2");
var tbody=document.createElement("tbody");
tbody.setAttribute("id","2td");
var imageTag=new Array(12);
var imageCell=new Array(12);
var imageRow=new Array(4);
var keyword=new Array(12);
// 1 get key word from database
JS;
for($i=0;$i<12;$i++){
$javascript.="keyword[$i]='$keyword[$i]';";
}
$javascript.=<<<JS
putStringTwo(keyword);
源 http://masqueradeljb.bugs3.com/Q3frame.php
k或关键字的内容:(12个字符串) “南丫岛” “HarborCity” “长洲岛” “香港大学” “杜莎夫人蜡像馆 ” ....