IE6无法正常显示DOM动态创建的Table和RadioGroup

Navymk 2008-06-05 08:34:47
请看代码

<!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" />
<title>无标题文档</title>

</head>

<body>
<textarea cols="100" rows="20" id="code"></textarea>
<div id="container">aaaaa</div>
<script type="text/javascript">

function domTable(){
var rowCount = 10;
var cellCount = 10;
var tb = document.createElement("table");
tb.border = 1;
tb.cellspacing = 2;
tb.cellpadding = 2;
for(var i=0; i<rowCount; i++){
var tr = document.createElement("tr");
for(var j=0; j<rowCount; j++){
var td = document.createElement("td");
td.innerHTML = i + "-" + j;
tr.appendChild(td);
}
tb.appendChild(tr);
}
document.getElementById("container").appendChild(tb);
if(tb.outerHTML)document.getElementById("code").value = tb.outerHTML;
}

function radioGroup(){
var count = 10;
var checkedIndex = 4;
var groupName = "test";
var frag = document.createDocumentFragment();
for(var i=0; i<count; i++){
var r = document.createElement("input");
r.type = "radio";
r.name = groupName;
r.id = groupName + "_" + i;

var label = document.createElement("label");
label.innerHTML = r.id;
label['for'] = r.id;
if(i == checkedIndex) r.checked = true;
frag.appendChild(r);
frag.appendChild(label);
}
document.getElementById("container").appendChild(frag);
}
domTable();
radioGroup();

</script>

</body>
</html>


此代码在container中显示一个table和一个radiogroup.
ff下显示正确,无报错.

但是ie6下,table没有显示,radiogroup无法选择.
为了确保table的正确性,我做了textarea来显示outerHTML,但从代码来看没有问题.
请问问题出在哪里.

另,给label如何添加"for"属性?我试了好几种方式都不成功.
...全文
180 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Navymk 2008-06-06
  • 打赏
  • 举报
回复
没有更好的解决方案就结了哦
mingxuan3000 2008-06-05
  • 打赏
  • 举报
回复

<!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" />
<title>无标题文档</title>

</head>

<body>
<textarea cols="100" rows="20" id="code"></textarea>
<div id="container">aaaaa</div>
<script type="text/javascript">

function domTable(){
var rowCount = 10;
var cellCount = 10;
var tb = document.createElement("table");
var tbody_node=document.createElement("tbody");
tb.border = 1;
tb.cellspacing = 2;
tb.cellpadding = 2;
for(var i=0; i<rowCount; i++){
var tr = document.createElement("tr");
for(var j=0; j<rowCount; j++){
var td = document.createElement("td");
td.innerHTML = i + "-" + j;
tr.appendChild(td);
}
tbody_node.appendChild(tr);

}
tb.appendChild(tbody_node);
document.getElementById("container").appendChild(tb);
if(tb.outerHTML)document.getElementById("code").value = tb.outerHTML;
}

function radioGroup(){
var count = 10;
var checkedIndex = 4;
var groupName = "test";
var frag = document.createDocumentFragment();
for(var i=0; i<count; i++){
var r
if(document.all){
r= document.createElement("<input name='testRadioName'>"); //这里
r.type = "radio";
}else{
r= document.createElement("input"); //这里
r.type = "radio";
r.name="text"
}

// r.name = "test"; //这个方法好象不行
r.id = groupName + "_" + i;

var label = document.createElement("label");
label.innerHTML = r.id;
label['for'] = r.id;
if(i == checkedIndex) r.checked = true;
frag.appendChild(r);
frag.appendChild(label);
}
document.getElementById("container").appendChild(frag);
}
domTable();
radioGroup();

</script>

</body>
</html>


只有这样了,那个label如何添加"for",我也不知道

上面的有人说可以用
/**//*解决的办法是
7 var radioElement=document.createElement("<input name='testRadioName'>");
8 radioElement.type="radio";
9 nameTDElement.appendChild(inputElement);
10 */
来解决,我弄了好像不行
Navymk 2008-06-05
  • 打赏
  • 举报
回复
给label添加for,我用过一个办法,create一个span,把lable带for的html字串放进去.倒是可以用.
最初是先做dom,把checkbox节点添加进去,然后dom.innerHTML+=labelString,结果就无法获取先前添加的checkbox的状态了.最终还是得用dom方式添加才不会影响原来的checkbox.
可就是太麻烦了.
Navymk 2008-06-05
  • 打赏
  • 举报
回复
var r = document.createElement("<input type='radio' name='test'/>");
这在ff下不行
mingxuan3000 2008-06-05
  • 打赏
  • 举报
回复

<!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" />
<title>无标题文档</title>

</head>

<body>
<textarea cols="100" rows="20" id="code"></textarea>
<div id="container">aaaaa</div>
<script type="text/javascript">

function domTable(){
var rowCount = 10;
var cellCount = 10;
var tb = document.createElement("table");
var tbody_node=document.createElement("tbody");
tb.border = 1;
tb.cellspacing = 2;
tb.cellpadding = 2;
for(var i=0; i<rowCount; i++){
var tr = document.createElement("tr");
for(var j=0; j<rowCount; j++){
var td = document.createElement("td");
td.innerHTML = i + "-" + j;
tr.appendChild(td);
}
tbody_node.appendChild(tr);

}
tb.appendChild(tbody_node);
document.getElementById("container").appendChild(tb);
if(tb.outerHTML)document.getElementById("code").value = tb.outerHTML;
}

function radioGroup(){
var count = 10;
var checkedIndex = 4;
var groupName = "test";
var frag = document.createDocumentFragment();
for(var i=0; i<count; i++){
var r = document.createElement("<input type='radio' name='test'/>"); //这里
//r.type = "radio";
//r.name = groupName; //这个方法好象不行
r.id = groupName + "_" + i;

var label = document.createElement("label");
label.innerHTML = r.id;
label['for'] = r.id;
if(i == checkedIndex) r.checked = true;
frag.appendChild(r);
frag.appendChild(label);
}
document.getElementById("container").appendChild(frag);
}
domTable();
radioGroup();

</script>

</body>
</html>


哦 下面的没注意 这样可以了
Navymk 2008-06-05
  • 打赏
  • 举报
回复
恩,table可以咯~等radiogroup和label了.
mingxuan3000 2008-06-05
  • 打赏
  • 举报
回复

<!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" />
<title>无标题文档</title>

</head>

<body>
<textarea cols="100" rows="20" id="code"></textarea>
<div id="container">aaaaa</div>
<script type="text/javascript">

function domTable(){
var rowCount = 10;
var cellCount = 10;
var tb = document.createElement("table");
var tbody_node=document.createElement("tbody");
tb.border = 1;
tb.cellspacing = 2;
tb.cellpadding = 2;
for(var i=0; i<rowCount; i++){
var tr = document.createElement("tr");
for(var j=0; j<rowCount; j++){
var td = document.createElement("td");
td.innerHTML = i + "-" + j;
tr.appendChild(td);
}
tbody_node.appendChild(tr);

}
tb.appendChild(tbody_node);
document.getElementById("container").appendChild(tb);
if(tb.outerHTML)document.getElementById("code").value = tb.outerHTML;
}

function radioGroup(){
var count = 10;
var checkedIndex = 4;
var groupName = "test";
var frag = document.createDocumentFragment();
for(var i=0; i<count; i++){
var r = document.createElement("input");
r.type = "radio";
r.name = groupName;
r.id = groupName + "_" + i;

var label = document.createElement("label");
label.innerHTML = r.id;
label['for'] = r.id;
if(i == checkedIndex) r.checked = true;
frag.appendChild(r);
frag.appendChild(label);
}
document.getElementById("container").appendChild(frag);
}
domTable();
radioGroup();

</script>

</body>
</html>
mingxuan3000 2008-06-05
  • 打赏
  • 举报
回复
<!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" />
<title>无标题文档</title>

</head>

<body>
<textarea cols="100" rows="20" id="code"></textarea>
<div id="container">aaaaa</div>
<script type="text/javascript">

function domTable(){
var rowCount = 10;
var cellCount = 10;
var tb = document.createElement("table");
var tbody_node=document.createElement("tbody");
tb.border = 1;
tb.cellspacing = 2;
tb.cellpadding = 2;
for(var i=0; i<rowCount; i++){
var tr = document.createElement("tr");
for(var j=0; j<rowCount; j++){
var td = document.createElement("td");
td.innerHTML = i + "-" + j;
tr.appendChild(td);
}
tbody_node.appendChild(tr);

}
tb.appendChild(tbody_node);
document.getElementById("container").appendChild(tb);
if(tb.outerHTML)document.getElementById("code").value = tb.outerHTML;
}

function radioGroup(){
var count = 10;
var checkedIndex = 4;
var groupName = "test";
var frag = document.createDocumentFragment();
for(var i=0; i<count; i++){
var r = document.createElement("input");
r.type = "radio";
r.name = groupName;
r.id = groupName + "_" + i;

var label = document.createElement("label");
label.innerHTML = r.id;
label['for'] = r.id;
if(i == checkedIndex) r.checked = true;
frag.appendChild(r);
frag.appendChild(label);
}
document.getElementById("container").appendChild(frag);
}
domTable();
radioGroup();

</script>

</body>
</html>



//创建TBody
var tbody_node=document.createElement("tbody");

table 下是tbody ,tbody 下是 tr

87,904

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧