setAttribute设置的属性无效??

whodsow 2004-04-28 02:11:32
页面生成的radio不能选择,class的设置也无效(如有效,背景为红色),怎么回事?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>DataGrid</title>
<style>
.class1
{ background-color: #ff0000 }
</style>

<script language="JavaScript">
function init()
{
dg = document.getElementById("DataGrid");
var tableBody = dg.getElementsByTagName("tbody").item(0);
for(var i = 0; i < 10; i++)
{
var tr = document.createElement("tr");
var td = document.createElement("td");
var radio = document.createElement("input");
radio.setAttribute("type", "radio");
radio.setAttribute("name", "group1");
radio.setAttribute("class", "class1");
radio.setAttribute("value", i + "");
td.appendChild(radio);
tr.appendChild(td);
tableBody.appendChild(tr);
}
dg.appendChild(tableBody);
}
</script>
</head>
<body onload="init()">
<table border="1" width="100%" id="DataGrid">
</table>
</body>
</html>
...全文
958 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wanghr100 2004-04-28
  • 打赏
  • 举报
回复
name加不上去.
所以,radio不能选择..
还是直接像阿信写的那种.
wanghr100 2004-04-28
  • 打赏
  • 举报
回复
单选还是多选啊??


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>DataGrid</title>
<style>
.class1
{ background-color: #ff0000 }
</style>

<script language="JavaScript">
function init()
{
dg = document.getElementById("DataGrid");
var tableBody = dg.getElementsByTagName("tbody").item(0);
for(var i = 0; i < 10; i++)
{
var tr = document.createElement("tr");
var td = document.createElement("td");
var radio1 = document.createElement("input");
radio1.type="radio";
radio1.name="group1";
radio1.className="class1";
radio1.value= i + "";
td.appendChild(radio1);
tr.appendChild(td);
tableBody.appendChild(tr);
}
dg.appendChild(tableBody);
}
document.onclick=function(){if(event.srcElement.value)event.srcElement.checked=!event.srcElement.checked}
</script>
</head>
<body onload="init()">
<table border="1" width="100%" id="DataGrid">
</table>
</body>
</html>
希默软件 2004-04-28
  • 打赏
  • 举报
回复
你看看,你设置的name相同,但可以单选么?

必须 document.createElement("<input name='aa'>");
whodsow 2004-04-28
  • 打赏
  • 举报
回复
不过name,似乎不可以这么做,只能 createElement 的时候
bencalie(Bencalie)什么意思??
希默软件 2004-04-28
  • 打赏
  • 举报
回复
不过name,似乎不可以这么做,只能 createElement 的时候
希默软件 2004-04-28
  • 打赏
  • 举报
回复
radio.setAttribute("className", "class1");
fason 2004-04-28
  • 打赏
  • 举报
回复
input是支持setAttribute的

var radio = document.createElement("span");
radio.innerHTML = "<input type=radio name=group1 class=class1 value="+i+">";
whodsow 2004-04-28
  • 打赏
  • 举报
回复
input肯定支持setAttribute。
LxcJie 2004-04-28
  • 打赏
  • 举报
回复
input 是不支持setAttribute的

87,996

社区成员

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

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