Create string from HTMLDivElement

前端 未结 6 1686
醉酒成梦
醉酒成梦 2020-12-29 06:27

What I would like to be able to do is create a string from a Javascript HTMLElement Object. For example:

var day = document.createElement(\"div\");
day.class         


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-29 07:09

    You need to create text node to add text for your created element like this:

    var day = document.createElement("div");
    day.className = "day";
    // create text node
    var txt = document.createTextNode('Random Text');
    // add text to div now
    day.appendChild(txt);
    // append to body
    document.body.appendChild(day);
    

提交回复
热议问题