Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Leonardo Pinho

Criação de linhas em uma tabela

Recommended Posts

Então pessoal,Eu estava querendo inserir algumas linhas quando o usuario quisesse, então eu criei um teste, mas não funciona!!!Essa é a minha página:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><script>	function test(){		alert(document.getElementsByName("id").length);		table = document.getElementById('tab');		tr = table.insertRow(table.rows.length);		td = tr.insertCell(0);		ipt = document.createElement('input');		ipt.type = 'text';		ipt.name = 'id';		ipt.value = '5';		td.appendChild(ipt);		alert(document.getElementsByName("id").length);		}</script></head><body><table id="tab"><tbody><tr><td><input type="text" name="id" value="1"><input type="text" name="id" value="2"><input type="text" name="id" value="3"><input type="text" name="id" value="4"></td></tr><tr><td><input type="button" onClick="test();" value="Inserir" ></td></tr></tbody></table></body></html>

quando ele entra, beleza ele pega o length, certo, mas quando esta terminando a function, ele não devolve o length,mais o input inserido!!!!Ele sempre mostra o length = 4!!!!!no FF funciona assim, mas no IE, não funciona, vcs poderiam me ajudar???Obrigado

Compartilhar este post


Link para o post
Compartilhar em outros sites

acho que é porque para ser crossbrowser você tem que inserir os atributos com o setAttribute, após criar o elemento faça isso:

 

nome_do_elemento.setAttribute(atributo,valor);

Compartilhar este post


Link para o post
Compartilhar em outros sites

Eu mudei:

<script>	function test(){		alert(document.getElementsByName("id").length);		table = document.getElementById('tab');		tr = table.tBodies[0].insertRow(table.rows.length);		td = tr.insertCell(0);		ipt = document.createElement('input');		ipt.setAttribute('type','text');		ipt.setAttribute('name','id');		ipt.setAttribute('value','5');		td.appendChild(ipt);		alert(document.getElementsByName("id").length);		}</script>

Mas mesmo assim, ela não "encherga" que foi inserido mais um input com o name='id'.

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.