This is an old revision of the document!
Continuaremos construyendo nuestra tabla de estadísticas sobre el equipo de Colombia, pero lo haremos de manera más eficiente con javascript usando matrices y bucles.
equipo.html
<table> <thead> <tr> <th>Nombre</th> <th>Apellido</th> <th>Nacionalidad</th> <th class="edad">Edad</th> <th>POsición</th> </tr> </thead> <tbody> </tbody> </table>
style.css
td, th { border-width: 1px; border-style: solid; } td { padding: 10px; } table { border-collapse: collapse; } #carlos { font-style: italic; } .age { background-color: yellow; } #carlos .age { font-weight: 900; } td.age { font-size: 150%; }
equipo.js
window.onload = function () { }
window.onload = function () { // generar datos del jugador jugador = ['David', 'Ospina Ramirez', 'Colombia', 33, 'Portero']; }
// crea texto de fila statsRow = "<tr>"; statsRow += "<td>" + jugador[0] + "</td>"; statsRow += "<td>" + jugador[1] + "</td>"; statsRow += "<td>" + jugador[2] + "</td>"; statsRow += "<td>" + jugador[3] + "</td>"; statsRow += "<td>" + jugador[4] + "</td>"; statsRow += "</tr>";
jugador[]
hace referencia a los datos en la lista llamada 'jugador'.// agregar fila a la tabla tableBody = document.getElementsByTagName("tbody")[0]; newRow = tableBody.insertRow(0); newRow.innerHTML = statsRow;
<tbody>
que se asigna a la variable 'tableBody'.<tbody>
) de nuestro elemento tableBody y lo asignamos a una variable llamada 'newRow'.newRow
la cadena de texto que creamos anteriormente.window.onload = funcion () { // generar datos jugador = ['David', 'Ospina Ramirez', 'Colombia', 33, 'Portero']; // crea texto de fila statsRow = "<tr>"; statsRow += "<td>" + jugador[0] + "</td>"; statsRow += "<td>" + jugador[1] + "</td>"; statsRow += "<td>" + jugador[2] + "</td>"; statsRow += "<td>" + jugador[3] + "</td>"; statsRow += "<td>" + jugador[4] + "</td>"; statsRow += "</tr>"; // agregar fila a la tabla tableBody = document.getElementsByTagName("tbody")[0]; newRow = tableBody.insertRow(0); newRow.innerHTML = statsRow; }
funcion crearTextoFila() { }
funcion crearTextoFila() { statsRow = "<tr>"; statsRow += "<td>" + jugador[0] + "</td>"; statsRow += "<td>" + jugador[1] + "</td>"; statsRow += "<td>" + jugador[2] + "</td>"; statsRow += "<td>" + jugador[3] + "</td>"; statsRow += "<td>" + jugador[4] + "</td>"; statsRow += "</tr>"; }
funcion crearTextoFila(jugador) { statsRow = "<tr>"; statsRow += "<td>" + jugador[0] + "</td>"; statsRow += "<td>" + jugador[1] + "</td>"; statsRow += "<td>" + jugador[2] + "</td>"; statsRow += "<td>" + jugador[3] + "</td>"; statsRow += "<td>" + jugador[4] + "</td>"; statsRow += "</tr>"; return statsRow; }