在嵌套元素生成javascript上获取错误

时间:2018-01-04 17:54:44

标签: javascript html css

我想用js函数改变div上的默认头,但是在我的代码的最终结果是文本[object HTMLDivElement]之后。 这是我的代码:

    function Modelo1(){
        var div=document.createElement('div');
        var logo = document.createElement('img');
        logo.height=80;
        logo.width=250;
        logo.src="persona-5-listing-thumb-01-ps4-us-30jun16.png";
        var datos= document.createElement('div');
        var ruc = document.createElement('span');
        var dir = document.createElement('span');
        var tel = document.createElement('span');
        ruc.innerHTML = "RUC: ";
        dir.innerHTML = "dirección aca";
        tel.innerHTML = "Telefono: ";
        datos.appendChild(ruc);
        datos.appendChild(dir);
        datos.appendChild(tel);
        var doc=document.createElement('div');
        doc.border="2px solid "+colorBorde;
        var rD = document.createElement('span');
        rD.innerHTML="ruc";
        var tD = document.createElement('p');
        tD.innerHTML = "HEADER";
        doc.appendChild(rD);
        doc.appendChild(tD);
        div.appendChild(logo);
        div.appendChild(datos);
        div.appendChild(doc);
        document.getElementById('cabecera').innerHTML = div;
    }

1 个答案:

答案 0 :(得分:0)

div.innerHTML = logo + datos + div;问题是什么

1)你不能总结html元素 2)而+ div+ doc



function Modelo1(){
        var div=document.createElement('div');
        var logo = document.createElement('img');
        logo.height=80;
        logo.width=250;
        logo.src="persona-5-listing-thumb-01-ps4-us-30jun16.png";
        var datos= document.createElement('div');
        var ruc = document.createElement('span');
        var dir = document.createElement('span');
        var tel = document.createElement('span');
        ruc.innerHTML = "RUC: ";
        dir.innerHTML = "dirección aca";
        tel.innerHTML = "Telefono: ";
        datos.appendChild(ruc);
        datos.appendChild(dir);
        datos.appendChild(tel);
        var doc=document.createElement('div');
        doc.border="2px solid black";
        var rD = document.createElement('span');
        rD.innerHTML="ruc";
        var tD = document.createElement('p');
        tD.innerHTML = "HEADER";
        doc.appendChild(rD);
        doc.appendChild(tD);
        div.appendChild(logo)
        div.appendChild(datos)
        div.appendChild(doc)
        document.getElementById('cabecera').appendChild(div);
    }
    
    Modelo1()

<header id="cabecera"></header>
&#13;
&#13;
&#13;