我正在使用以下代码,我试图将嵌套的html组件附加到html元素
import java.util.*;
public class Race {
public static void main(String []args) {
int finish=70,tort=1,hare=1,rtime=0;
System.out.println("ON YOUR MARK, GET SET\nBANG !!!!!\nAND THEY'RE OFF !!!!!\n");
do{
hare=movehare(hare);
tort=movetort(tort);
print(tort,hare);
rtime++;
}
while(tort<finish&&hare<finish);
if(tort>hare ){
System.out.println("\nTORTOISE WINS!\n");
}
else if(tort<hare ){
System.out.println("\nHARE WINS!\n");
}
else{
System.out.println("IT\'S A TIE!\n");
}
}
public static void print(int t,int h){
int i;
if(h==t){
for(i=0;i<h;i++)
System.out.print("_");
System.out.println("OUCH!!!");
}
else if(h<t){
for(i=0;i<h;i++)
System.out.print("_");
System.out.print("H");
for(i=0;i<(t-h);i++)
System.out.print("_");
System.out.print("T");
}
else{
for(i=0;i<t;i++)
System.out.print("_");
System.out.print("T");
for(i=0;i<(h-t);i++)
System.out.print("_");
System.out.print("H");
}
System.out.println();
}
public static int movehare(int r ){
int num;
num=(int)(Math.random()*10);
if(num<2){
r-=2;
}
else if(num<5){
r++;
}
else if(num<6){
r-=12;
}
else if(num<8){
r+=9;
}
if(r< 1 ){
r=1;
}
return r;
}
public static int movetort(int t){
int num;
num=(int)(Math.random()*10);
if(num<5){
t+=3;
}
else if(num<7){
t-= 6;
}
else{
t++;
}
if(t<1){
t=1;
}
return t;
}
}
它正在迭代一个对象数组,我从中获取要添加到var docFrag = document.createDocumentFragment();
var count = 0;
arr.map(function(obj){
count++;
var title = obj.title;
var codeAttr = obj.code;
var htmlComponent = "<div class='line_h'><div class='floatRadio'><label for='radio_"+count+"'><input name='radio' value='"+codeAttr+"' id='radio_"+count+"' checked='checked' type='radio' class='inputRadio' /><span class='fld_lbl'>"+title+"</span></label></div></div>";
console.log(htmlComponent);
docFrag.appendChild(htmlComponent);
})
的内容。但是,在执行htmlComponent
时,它会返回错误:
无法在'Node'上执行'appendChild':参数1的类型不是'Node'。
确实我传递了一个字符串......我怎样才能构建一个嵌套的html组件并将其推送到docFrag.appendChild(htmlComponent)
?