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;     
  •     } 
posted @ 2014-12-24 10:15  hannover  阅读(7537)  评论(0编辑  收藏  举报