js动态创建HTML(radio、checkbox...)[摘抄]

时间:2023-03-09 16:53:59
js动态创建HTML(radio、checkbox...)[摘抄]
  • function create(parentId,eleType,eleName,eleId,eleValue){
  • var board = document.getElementById(parentId);
  • var e = createElement("input",eleName);
  • e.type = eleType;
  • e.id = eleId;
  • e.value = elevalue;
  • board.appendChild(e);
  • //设置选中
  • e.setAttribute("checked","checked");
  • //添加文字
  • board.appendChild(document.createTextNode("Item"));
  • }
  • function createElement(type, name) {
  • var element = null;
  • try {
  • // First try the IE way; if this fails then use the standard way
  • element = document.createElement_x('<'+type+' name="'+name+'">');
  • } catch (e) {
  • // Probably failed because we’re not running on IE
  • }
  • if (!element) {
  • element = document.createElement(type);
  • element.name = name;
  • }
  • return element;
  • }