Html 使用javascript创建动态div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19494339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 14:39:38  来源:igfitidea点击:

Creating dynamic div using javascript

javascripthtml

提问by Anoop

 <script>
    function selecteditems()
    {

    var i=1;
    var val="";
    while(i<=53)
    {

        if(document.getElementById('timedrpact'+i)!="")
        {
            val+=document.getElementById('timedrpact'+i).value;
            document.getElementById('showselecteditems').innerHTML=val;
            }
        i++;
        }

    }
    </script>

How to create a new div and add contents to it?In the above case i lost previous content due to innerHTML.I want new div each time for dynamically attach an image and the above variable val to it. The desired and current output demonstrated here

如何创建一个新的 div 并向其添加内容?在上述情况下,由于innerHTML,我丢失了以前的内容。我每次都希望新的 div 动态附加图像和上述变量 val 到它。 此处演示的所需输出和当前输出

Thanks in advance.

提前致谢。

采纳答案by Shadow

Check this Demo

检查这个演示

<div id="output" class="out">

</div>

window.onload=function(){
    var output = document.getElementById('output');
    var i=1;
    var val="";
    while(i<=3)
    {

        if(!document.getElementById('timedrpact'+i))
        {
            var ele = document.createElement("div");
            ele.setAttribute("id","timedrpact"+i);
            ele.setAttribute("class","inner");
            ele.innerHTML="hi "+i;
            output.appendChild(ele);

        }
        i++;


    }
};

回答by Cobra_Fast

Look at document.createElement()and element.appendChild().

看看document.createElement()element.appendChild()

var newdiv = document.createElement("div");
newdiv.innerHTML = val;
document.getElementById("showselecteditems").appendChild(newdiv);


Because you will likely encounter this in the near future: You can remove any elementwith this code:

因为在不久的将来您可能会遇到这种情况:您可以element使用以下代码删除任何内容:

element.parentNode.removeChild(element);

回答by mplungjan

Using createElement:

使用createElement

function selecteditems() {
  var container = document.getElementById('showselecteditems');
  for (var i=1;i<=53;i++) {
    var fld = document.getElementById('timedrpact'+i);
    if (fld) {
      var div = document.createElement("div");
      div.appendChild(document.createTextNode(fld.value));
      container.appendChild(div);
    }
  }
}

Full version using cloneNode (faster) and eventBubbling

使用 cloneNode(更快)和 eventBubbling 的完整版本

Live Demo

Live Demo

var div = document.createElement("div");
var lnk = document.createElement("a");
var img = document.createElement("img")
img.className="remove";
img.src = "https://uperform.sc.gov/ucontent/e14c3ba6e4e34d5e95953e6d16c30352_en-US/wi/xhtml/static/noteicon_7.png";
lnk.appendChild(img);
div.appendChild(lnk);
function getInputs() {
  var container = document.getElementById('showselecteditems');
  for (var i=1;i<=5;i++) {
    var fld = document.getElementById('timedrpact'+i);
    if (fld) {
      var newDiv = div.cloneNode(true);
      newDiv.getElementsByTagName("a")[0].appendChild(document.createTextNode(fld.value));
      container.appendChild(newDiv);
    }
  }
}    
window.onload=function() {
  document.getElementById('showselecteditems').onclick = function(e) {
    e=e||event; 
    var target = e.target||e.srcElement; 
    // target is the element that has been clicked
    if (target && target.className=='remove') {
      parentDiv = target.parentNode.parentNode;
      parentDiv.parentNode.removeChild(parentDiv);
      return false; // stop event from bubbling elsewhere
    }
  }    
  getInputs();  
}    

回答by bgs

Syntax for dynamic create div:

动态创建 div 的语法:

  DivId =  document.createElement('div');
  DivId.innerHtml ="text"