在我的主頁我有這五個div的我如何才能簡化此Javascript有類似事件
<table id="nav">
<tr>
<td><a href="coding.html"><div id="pagecl">C O D I N G</div></a></td>
<td><div id="pagecl" class="high">A R T W O R K</div></td>
<td><div id="logo"><img src="imageswebbing/icon.png"></div></td>
<td><a href="extras.html"><div id="pagecl" class="more">E X T R A S</div></a></td>
<td><a href="about.html"><div id="pagecl" class="last">A B O U T</div></a></td>
</tr>
</table>
這是對應的CSS:
#nav {
margin:300px auto auto auto;
}
#pagecl {
height:40px;
width:200px;
background-color:#151515;
color:white;
text-align:center;
line-height:40px;
opacity:0.7;
font-family: Garamond;
font-size:12px;
}
#logo {
height:120px;
width:120px;
}
和JavaScript允許慢衰落與鼠標進入和鼠標離開,但如果我只在javascript中使用#pagecel id,則預期效果只能在第一個「編碼」div中看到。這就是爲什麼我在html和javascript中添加其他div的類選擇器。我如何壓縮這個?可能有一個簡單的解決方案;我爲成爲一名新手而道歉。
$(document).ready(function() {
$('#pagecl').mouseenter(function() {
$(this).fadeTo("slow",1);
});
$('#pagecl').mouseleave(function() {
$(this).fadeTo("slow",.7);
});
$('.high').mouseenter(function() {
$(this).fadeTo("slow",1);
});
$('.high').mouseleave(function() {
$(this).fadeTo("slow",.7);
});
$('.more').mouseenter(function() {
$(this).fadeTo("slow",1);
});
$('.more').mouseleave(function() {
$(this).fadeTo("slow",.7);
});
$('.last').mouseenter(function() {
$(this).fadeTo("slow",1);
});
$('.last').mouseleave(function() {
$(this).fadeTo("slow",.7);
});
});
ID是唯一的,你不能使用他們不止一次。 – adeneo
要擴展@ adeneo的評論,儘管您可以*將多個位置的ID寫入您的標記,並將其視爲*有效*標記,您應該爲唯一項目使用ID(因此每個實例只能使用一次)。任何落入可重複單位的東西都應該用類名指定。多次使用相同的ID可能會導致腳本問題,而不僅僅是簡單的驗證失敗。 –