我正在尝试获取一个悬停在页面顶部的表格,以便在您将鼠标悬停在其上时更改颜色。我在 div 上使用以下 CSS:
#navbar {
width: 100%;
margin: 0;
position: fixed;
top: 0px;
border-spacing: 0;
}
#navbar td {
width: 20%;
background: #3399ff;
padding: 10px;
text-align: center;
font-size: 24px;
font-family: "Verdana", sans-serif;
font-variant: small-caps;
}
#navbar td:hover {
cursor: pointer;
}
这是表本身:
<table id="navbar">
<tr>
<td data-href="/">
<div>
Home
</div>
</td>
<td data-href="/tribes">
<div>
Tribes
</div>
</td>
<td data-href="/minecraft">
<div>
Minecraft
</div>
</td>
<td data-href="/dynmap">
<div>
Dynmap
</div>
</td>
<td data-href="/links">
<div>
Links
</div>
</td>
</tr>
</table>
这是javascript:
$(document).ready(function () {
$('#edit').click(function () {
$('#editformdiv').slideToggle();
$('#password').focus();
});
$("#navbar td").click(function () {
location.href = this.getAttribute("data-href");
});
$("#navbar td").mouseenter(function () {
$(this).css('background','#33ccff')
});
$('#navbar td').mouseleave(function () {
$(this).css('background','#3399ff');
});
});
这有效,表格卡在页面顶部。但是,将鼠标悬停在表格单元格上时,它不会改变颜色。我该如何解决这个问题,以便表格单元格在悬停时改变颜色?
编辑:这是整个页面:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Home</title>
<script src="/jquery-1.9.1.min.js"></script>
<script src="/scripts.js"></script>
<link rel="stylesheet" type="text/css" href="/stylesheet.css">
<style>
#editcontainer {
width: 100%;
margin-left: auto;
margin-right: auto;
position: fixed;
bottom: 0px;
margin-bottom: 0;
}
#edit {
padding: 4px;
width: 100px;
background-color: #3399ff;
margin-bottom: 0;
text-align: center;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
margin-left: auto;
margin-right: auto;
}
#editformdiv {
margin-top: 0;
border: 2px solid #3399ff;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
border-bottom: 0;
margin-left: auto;
margin-right: auto;
display: none;
width: 300px;
background-color: white;
}
#editform {
text-align: center;
}
#edit:hover {
cursor: pointer;
}
</style>
</head>
<body>
<table id="navbar">
<tr>
<td data-href="/">
Home
</td>
<td data-href="/tribes">
Tribes
</td>
<td data-href="/minecraft">
Minecraft
</td>
<td data-href="/downloads">
Downloads
</td>
<td data-href="/links">
Links
</td>
</tr>
</table>
<div class="spacer"></div>
<div class="spacer"></div>
<div id="editcontainer">
<div id="edit">EDIT</div>
<div id="editformdiv">
<table id="editform">
<form name="login" method="post" action="edit.php">
<tr>
<td><b>Login</b></td>
<td></td>
</tr>
<tr>
<td>Password:</td>
<td><input name="password" type="password" id="password"></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Login"></td>
<td></td>
</tr>
</form>
</table>
</div>
</div>
</body>
</html>
这是整个 CSS 表:
body {
margin: 0;
width: 100%;
font-family: "Verdana", sans-serif;
font-size: 14px;
}
#navbar {
width: 100%;
margin: 0;
position: fixed;
top: 0px;
border-spacing: 0;
}
#navbar td{
width: 20%;
background-color: #3399ff;
padding: 10px;
text-align: center;
font-size: 24px;
font-family: "Verdana", sans-serif;
font-variant: small-caps;
}
#navbar td:hover {
cursor: pointer;
background-color: #33ccff;
}
.spacer {
opacity: 0;
height: 50px;
}
.text {
width: 65%;
padding: 5px;
border: 2px solid #3399ff;
border-radius: 5px;
margin: 10px auto;
}
div h2 {
font-size: 18px;
font-weight: normal;
text-align: center;
}
.text p:last-child {
color: gray;
text-align: center;
}