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

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

问题描述

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

Can I pass a variable in hover()?

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

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();

    });

所以我尝试像这样传递var },function (target) {,当然是错误的,但是还有什么其他的方法可以传递这个var呢?

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

谢谢.

推荐答案

短版只是在这里切换:

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

为了让它在每个处理程序中可用(作为更通用的解决方案)在循环时在外部定义它(使用 .each() 例如),像这样:

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:在悬停()函数中传递变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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