我创建了一个程序,该程序用名为usuarios的表的数据填充表,但是属性nth-child似乎无法正常工作
所有行都具有相同的颜色,为黄色。
。我正在使用Netbeans和Glassfish
<%--
Document : basededatos
Created on : 14/06/2015, 01:23:57
Author : fernando
--%>
<%@page import="java.sql.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<style type="text/css">
.TFtable{
width:100%;
border-collapse:collapse;
}
.TFtable td{
padding:7px; border:#4e95f4 1px solid;
}
/* provide some minimal visual accomodation for IE8 and below */
.TFtable tr{
background: red;
}
/* Define the background color for all the ODD background rows */
.TFtable tr:nth-child(odd){
background: yellow;
}
/* Define the background color for all the EVEN background rows */
.TFtable tr:nth-child(even){
background: greenyellow;
}
</style>
</head>
<body>
<%
Connection conn = null;
ResultSet result = null;
Statement stmt = null;
ResultSetMetaData rsmd = null;
try {
Class.forName("com.mysql.jdbc.Driver");
out.println("driver registrado");
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/administracion", "root", "strlen12");
out.println("conexion exitosa");
stmt = conn.createStatement();
result = stmt.executeQuery("select * from usuarios");
%>
<table border="1" class="TFtable">
<caption>Lista de Usuarios</caption>
<tr style="background-color: grey">
<td>Nombre</td>
<td>Codigo</td>
</tr>
<%
while (result.next()) {
%>
<tr>
<td>
<% out.print(result.getString(1)); %>
</td>
<td>
<% out.print(result.getString(2)); %>
</td>
<tr>
<%
}
%>
</table>
<%
} catch (Exception e) {
out.print(e);
}
%>
</body>
</html>
最佳答案
问题是您没有关闭tr
标记
改变这个:
<tr>
<td>
<% out.print(result.getString(1)); %>
</td>
<td>
<% out.print(result.getString(2)); %>
</td>
<tr>
至:
<tr>
<td>
<% out.print(result.getString(1)); %>
</td>
<td>
<% out.print(result.getString(2)); %>
</td>
</tr>