繁体   English   中英

用javascript添加html文本

[英]add html text with javascript

我正在使用javascript函数添加输入文件标签和链接,效果很好。 现在我还想添加一个单选按钮和该收音机的文本...我可以添加收音机,没有问题,但是文本... idk怎么做。

这是代码...

addCampo = function () {  
    nDiv = document.createElement('div');
    nDiv.className = 'archivo';
    nDiv.id = 'file' + (++numero);


    nCampo = document.createElement('input');
    nCampo.name = 'archivos[]';
    nCampo.type = 'file';

    a = document.createElement('a');
    a.name = nDiv.id;
    a.href = '#';
    a.onclick = elimCamp;
    a.innerHTML = ' Eliminar';

    portada = document.createElement('input');
    portada.name = 'portada';
    portada.type = 'radio';
    portada.value = '1';

    nDiv.appendChild(nCampo);
    nDiv.appendChild(portada);

    // HERE I WANT A SIMPLE TEXT SAYING WHATS DOES THE RADIO =) 

    nDiv.appendChild(a);

    container = document.getElementById('adjuntos');
    container.appendChild(nDiv);
}

这很好用! 我唯一不知道的是如何添加文本标记标签...

你需要

text = document.createTextNode('what the radio does');
nDiv.appendChild(text);

尽管最好使用标签,但因为这样您就不必精打细选单选按钮。 在这种情况下,您需要:

portada.id = 'portada';
text = document.createElement('label');
text.innerText = 'what the radio does';
text.for = 'portada';
nDiv.appendChild(text);

编辑:如评论中所述,不一定所有浏览器都支持innerText,对不起! 只需使用innerHTML即可,如果您不关心IE的旧版本,请使用textContent,或者创建一个文本节点并将其添加到标签节点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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