gpt4 book ai didi

jQuery Animate - 导航

转载 作者:行者123 更新时间:2023-12-01 01:15:41 34 4
gpt4 key购买 nike

我正在使用 jQuery 函数 .animate 来一一突出显示导航链接。他们在 ul 中。我可以让它工作,只是想知道是否有一种方法可以缩短我的代码,这样我就不必单独突出显示每个项目。提前致谢

$(document).ready(function(){
$('#button1').mouseenter(function(){
$(this).animate({
color:"#FFFFFF",
backgroundColor: "#FF9B05"
});
});
$('#button1').mouseleave(function(){
$(this).animate({
color:"#FF9B05",
backgroundColor: "#FFFFFF"
});
});
$('#button2').mouseenter(function(){
$(this).animate({
color:"#FFFFFF",
backgroundColor: "#FF9B05"
});
});
$('#button2').mouseleave(function(){
$(this).animate({
color:"#FF9B05",
backgroundColor: "#FFFFFF"
});
});
$('#button3').mouseenter(function(){
$(this).animate({
color:"#FFFFFF",
backgroundColor: "#FF9B05"
});
});
$('#button3').mouseleave(function(){
$(this).animate({
color:"#FF9B05",
backgroundColor: "#FFFFFF"
});
});
$('#button4').mouseenter(function(){
$(this).animate({
color:"#FFFFFF",
backgroundColor: "#FF9B05"
});
});
$('#button4').mouseleave(function(){
$(this).animate({
color:"#FF9B05",
backgroundColor: "#FFFFFF"
});
});
});

最佳答案

将所有选择器合并到一个语句中,然后附加事件监听器:

$('#button1, #button2, #button3, #button4').mouseenter(function(){
$(this).animate({
color:"#FFFFFF",
backgroundColor: "#FF9B05"
});
}).mouseleave(function(){
$(this).animate({
color:"#FF9B05",
backgroundColor: "#FFFFFF"
});
});

关于jQuery Animate - 导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17030131/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com