0

我有这个代码:

$('#services_list > div').hover(function() {

    var serviceHoveredOn = '#' + $(this).attr('id');
    $(serviceHoveredOn + ' .service_expand').stop().animate({'width': '169px'}, 150);

},

function() {

    $(serviceHoveredOn + ' .service_expand').stop().animate({'width': '159px'}, 150);

});

但是,serviceHoveredOn当我将鼠标悬停在div. 我搜索并发现了这个: jquery:在悬停()函数中传递变量? 但我不确定如何使用 toggle() 来解决我的问题。谢谢你。

4

1 回答 1

1

您不能访问在另一个函数中声明的变量。


在你的情况下,你甚至不需要它。只需使用this,并将其作为上下文传递:

$('#services_list > div').hover(function() {
    $('.service_expand', this).stop().animate({'width': '169px'}, 150);
},
function() {
    $('.service_expand', this).stop().animate({'width': '159px'}, 150);
});
于 2013-01-31T01:21:23.307 回答