想象我有這樣的HTML代碼段4倍以上的菜單導航凝結jQuery的鼠標懸停和鼠標移開
<ul class="navigation">
<li class="nav-Mens"><a href="#">Mens</a></li>
<li class="nav-Womens"><a href="#">Womens</a></li>
<li class="nav-Kids"><a href="#">Kids</a></li>
<li class="nav-Gear"><a href="#">Gear</a></li>
</ul>
<div id="Gear">
<ul class="Gear">
<table width="300" border="0" cellspacing="5" cellpadding="0">
<tbody><div style="background-color:#333; color:#000">SHOP GEAR</div>
<tr>
<td><a href="#"><b>Snow</b></a>
<p></p>
<li><a href="#">Bags</a></li></td>
<td><a href="#"><b>Surf</b></a>
<p></p>
<li><a href="#">Towels</a></li></td>
</tr>
</tbody></table>
</ul>
</div>
的四個不同的部分和我的jQuery看起來像這樣
// JavaScript Document
$(document).ready(function() {
//Mens
$("li.nav-Mens").mouseover(function() {
clearTimeout(timeout);
$('#Mens').show();
$('#Womens').hide();
$('#Kids').hide();
$('#Gear').hide();
});
var timeout;
$("li.nav-Mens").mouseout(function() {
timeout = setTimeout('hideMens()', 1000);
});
$('#Mens').mouseover(function() {
clearTimeout(timeout);
});
$('#Mens').mouseout(function() {
timeout = setTimeout('hideMens()', 1000);
});
//Womens
$("li.nav-Womens").mouseover(function() {
clearTimeout(timeout);
$('#Womens').show();
$('#Mens').hide();
$('#Kids').hide();
$('#Gear').hide();
});
var timeout;
$("li.nav-Womens").mouseout(function() {
timeout = setTimeout('hideWomens()', 1000);
});
$('#Womens').mouseover(function() {
clearTimeout(timeout);
});
$('#Womens').mouseout(function() {
timeout = setTimeout('hideWomens()', 1000);
});
//Kids
$("li.nav-Kids").mouseover(function() {
clearTimeout(timeout);
$('#Womens').hide();
$('#Mens').hide();
$('#Kids').show();
$('#Gear').hide();
});
var timeout;
$("li.nav-Kids").mouseout(function() {
timeout = setTimeout('hideKids()', 1000);
});
$('#Kids').mouseover(function() {
clearTimeout(timeout);
});
$('#Kids').mouseout(function() {
timeout = setTimeout('hideKids()', 1000);
});
//Gear
$("li.nav-Gear").mouseover(function() {
clearTimeout(timeout);
$('#Womens').hide();
$('#Mens').hide();
$('#Kids').hide();
$('#Gear').show();
});
var timeout;
$("li.nav-Gear").mouseout(function() {
timeout = setTimeout('hideGear()', 1000);
});
$('#Gear').mouseover(function() {
clearTimeout(timeout);
});
$('#Gear').mouseout(function() {
timeout = setTimeout('hideGear()', 1000);
});
});
//Calling all the functions to hide everything
function hideMens() {
$('#Mens').hide();
}
function hideWomens() {
$('#Womens').hide();
}
function hideKids() {
$('#Kids').hide();
}
function hideGear() {
$('#Gear').hide();
}
任何想法如何使jquery有點小,但更小?
非常感謝! – 2009-08-14 21:18:42