yzx99

导航

 

原来只知道document.createElement后面带HTML元素这种用法,如
  var oTD2=document.createElement("TD");
今天要对增加的元素使用document.getElementsByName获取其数组,因此对增加的元素name属性的设置:
 var oCk1=document.createElement("INPUT");
 oCk1.type="checkbox";
 oCk1.name="ChkSel";
 //oCk1.setAttribute("name", "ChkSel");

但以上代码却没有成功(也没有出错)。


开头还以为是bug之类的,后来查createElement的帮助,发现一句话
Attributes can be included with the sTag as long as the entire string is valid HTML. You should do this if you wish to include the NAME attribute at run time on objects created with the createElement method.
就是说document.createElement带的参数不只是一个HTML元素,也可以用HTML,这样,name属性就可以加入,例如:
var newRadioButton = document.createElement("<INPUT TYPE='RADIO' NAME='RADIOTEST' VALUE='First Choice'>")

因此,我的代码改成如下即可:
 var oCk1=document.createElement("<input type=\"checkbox\" name=\"ChkSel\" />");
 oTD1.appendChild(oCk1);


 

posted on 2008-08-23 08:36  yzx99  阅读(1636)  评论(0编辑  收藏  举报