你试过把它放在一个
$(document).ready(function() {
$('#fish').hover(function() {
$('#fish').css("background-image", "url(http://testsite.perseocigar.com/menu_hover.png)");
}, function() {
$('#fish').css("background-image", "url(http://testsite.perseocigar.com/menu_nohover.png)");
});
});
如果您想对多个元素使用相同的代码。不要这样做
$(document).ready(function() {
$('#fish').hover(function() {
$('#fish').css("background-image", "url(images/menu_hover.png)");
}, function() {
$('#fish').css("background-image", "url(images/menu_nohover.png)");
});
});
$(document).ready(function() {
$('#fish1').hover(function() {
$('#fish1').css("background-image", "url(images/menu_hover.png)");
}, function() {
$('#fish1').css("background-image", "url(images/menu_nohover.png)");
});
});
你可以像这样把它们放在一起
$(document).ready(function() {
$('#fish,#fish2,#fish3,#fish4,#fish5').hover(function() {
$(this).css("background-image", "url(images/menu_hover.png)");//<-- Then use 'this' to refer to the current element firing the event
}, function() {
$(this).css("background-image", "url(images/menu_nohover.png)");
});
});
或者甚至更好地给你的元素一个他们共享的类。
<div id="fish" class="fishhover">test 123</div>
<div id="fish1" class="fishhover">test 123</div>
<div id="fish2" class="fishhover">test 123</div>
<div id="fish3" class="fishhover">test 123</div>
<div id="fish4" class="fishhover">test 123</div>
然后按班级选择它们
$(document).ready(function() {
$('.fishhover').hover(function() {
$(this).css("background-image", "url(images/menu_hover.png)");//<-- Then use 'this' to refer to the current element firing the event
}, function() {
$(this).css("background-image", "url(images/menu_nohover.png)");
});
});