用 javascript 追加 HTML 代码

因为一个项目的需要,所以在网上找了好段时间,最后终于做出来了.

如下:

var aaa = document.createElement(' 这里放 HTML 代码 '); //创建元素

document.getElementById("目标对象id").appendChild(aaa); //在最后边追加

document.getElementById("主对象id").removeChild(document.getElementById("要移除的对象id"));

例:

var fileSum = 5;

function $(objID)
{
     return document.getElementById(objID);
}

//增加一个文件上传框
function addFile()
{
     if ((fileSum + delFileSum) > showSum)
     {
      showSum++;
      var aaa = document.createElement('<center id="fileName_'+showSum+'"><div></div></center>');
      aaa.innerHTML = '文件:<input name="fileName'+showSum+'" type="file" id="propfile_file"><input type="button" onclick="delFile(\''+showSum+'\');" value="删除"><select name="fileType"><option value="0" selected>其他格式</option><option value="1">CSV格式</option></select>';
      $("upLoadFileList").appendChild(aaa);
      $("fileObjNameList").value += 'fileName'+showSum+';';
     }
     else
     {
      alert('对不起,目前一次只允许上传 '+fileSum+' 个文件!');
     }
}  

//移除指定 num 的文件上传框
function delFile(num)
{
     $("upLoadFileList").removeChild($("fileName_"+num));
     delFileSum += 1;
     var fileObjNameList = new Array();
     var fileObjNameListNewContent = "";
     fileObjNameList = $("fileObjNameList").value.split(";");
     var i = 0;
     while(i < fileObjNameList.length)
     {
      if (fileObjNameList[i] != ("fileName"+num) && fileObjNameList[i] != "")
      {
       fileObjNameListNewContent += fileObjNameList[i] + ";";
      }
      i++;
     }
     //fileObjNameListNewContent = fileObjNameListNewContent.substring(1,fileObjNameListNewContent.length);
     $("fileObjNameList").value = fileObjNameListNewContent;
}

有什么看不懂的可以留言问我。.呵呵~~!!

评论

此博客中的热门博文

时间管理:每天挤出一小时

你的妈妈已经等了二十几年

教您如何认识植柔皮,头层皮革,修面皮革,油蜡皮、水染皮、摔纹皮、纳帕皮、打蜡皮、压花皮、修面皮、漆光皮、磨砂皮、贴膜皮