jquery:在hover()函数中传递变量? [英] jquery: passing variables in hover() function?

查看:130
本文介绍了jquery:在hover()函数中传递变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在hover()中传递一个变量吗?



在下面的脚本中,我不想声明同一个变量两次 var target = xxx 并且我不想让这个变量成为全局的 target = xxx bcos我有其他函数使用这个变量名 - 目标。

  $('。image-profile')。hover(function(){

var target = $('。button-change-image-profile',this);
target.show();

},function(){

/ / var target = $('。button-change-image-profile',this);
target.hide();

});

所以我试图像这样传递变量},function(target) {,当然这是错的,但是通过这个var的任何其他方法?



谢谢。

  $ 

('.image-profile')。hover(function(){
$('。button-change-image-profile',this).toggle();
});

要在每个处理程序中使用它(作为更通用的解决方案)例如 .each() ),如下所示:

  $('。image-profile')。each(function(){
var target = $('。button- (){
target.show();
},function(){
target());
$(this).hover .hide();
});
});


Can I pass a variable in hover()?

As in the script below, I don't want to declare the same variable twice var target = xxx and I don't want to make this variable a global target = xxx bcos I have other function using this variable name - target.

   $('.image-profile').hover(function () {

        var target = $('.button-change-image-profile',this);
        target.show();

    },function () {

        //var target = $('.button-change-image-profile',this);
        target.hide();

    });

So I tried to pass the var like this },function (target) {, of course it is wrong, but any other method to pass this var?

thanks.

解决方案

The short version is just to toggle here:

$('.image-profile').hover(function () {
    $('.button-change-image-profile',this).toggle();
});

To have it available in each handler (as a more general solution) define it outside when looping (using .each() for example), like this:

$('.image-profile').each(function() {
    var target = $('.button-change-image-profile',this);
    $(this).hover(function () {
        target.show();
    },function () {
        target.hide();
    });
});

这篇关于jquery:在hover()函数中传递变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆