Jquery 将鼠标悬停在alink上并设置div动画
嗨,为了得到我工作所需要的东西,我已经停下来了 基于:Jquery 将鼠标悬停在alink上并设置div动画,jquery,Jquery,嗨,为了得到我工作所需要的东西,我已经停下来了 基于: $(".test a").hover(function(){ $(this).toggle({ height: "200px" }); }, function() { var currentTab = $(this).attr('href'); $(this).stop(true, false).animate({ height: "100px" }); }); }); &l
$(".test a").hover(function(){
$(this).toggle({ height: "200px" });
}, function() {
var currentTab = $(this).attr('href');
$(this).stop(true, false).animate({ height: "100px" });
});
});
<div class="test">
<a href="#one">1</a>
<a href="#two">2</a>
<a href="#three">3</a>
</div>
您需要重新声明变量activeTab,因为它只存在于mouseenter的作用域中,而不存在mouseout
$(".test a").hover(function(){
var activeTab = $(this).attr("href");
$(activeTab).toggle({ height: "200px" });
},
function() {
var activeTab = $(this).attr("href");
$(activeTab).stop(true, false).animate({ height: "100px" });
});
});
这里有一些错误。。。一是,数一数你的括号
$(".test a").hover(function(){
var activeTab = $(this).attr("href");
$(activeTab).toggle({ height: "200px" });
},
function() {
$(activeTab).stop(true, false).animate({ height: "100px" });
});
}); // too many closing brackets
第二个是var activeTab超出了第二个函数的作用域。因此,您需要在第二个函数中重新定义var activeTab:
这应该起作用:
$(".test a").hover(function(){
var activeTab = $(this).attr("href");
$(activeTab).toggle({ height: "200px" });
},
function() {
var activeTab = $(this).attr("href");
$(activeTab).stop(true, false).animate({ height: "100px" });
});
$(".test a").hover(function(){
var activeTab = $(this).attr("href");
$(activeTab).toggle({ height: "200px" });
},
function() {
$(activeTab).stop(true, false).animate({ height: "100px" });
});
}); // too many closing brackets
$(".test a").hover(function(){
var activeTab = $(this).attr("href");
$(activeTab).toggle({ height: "200px" });
},
function() {
var activeTab = $(this).attr("href");
$(activeTab).stop(true, false).animate({ height: "100px" });
});