简体   繁体   中英

Javascript Appendchild

This is my code

for(var i=0;i<Year12.length;i++)
      {

                var div1=document.createElement('li');
                 div1.setAttribute('id',Year12[i]);
                 div1.setAttribute('Value',Year12[i]);
                 document.getElementById("Jan").appendChild(div1);
                 alert(div1);


       }

"jan" is the id of an UL element.I am able to create the Element. But Its not adding to the parent element.Can any one try this

Remove Value and try:

var div1=document.createElement('li');
div1.setAttribute('id',Year12[i]);
div1.innerHTML= Year12[i];
document.getElementById("Jan").appendChild(div1);
alert(div1);

OR

var div1=document.createElement('li');
    div1.setAttribute('id',Year12[i]);
    div1.appendChild(document.createTextNode(Year12[i]));
    document.getElementById("Jan").appendChild(div1);
    alert(div1);

For setting class, you can do:

div1.className = "your_class_name";

try this

for(var i=0;i<Year12.length;i++)
{

    var div1=document.createElement('li');
    div1.setAttribute('id',Year12[i]);
    div1.setAttribute('Value',Year12[i]);
    //document.getElementById("Jan").appendChild(div1);
    document.getElementById("Jan").append(div1);
    alert(div1);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM