如何在javascript的hover函数中传递变量名

时间:2022-01-06 20:43:17

want to pass variable name in hover function of javascript

想在javascript的hover函数中传递变量名

     $(".icon a").hover(function() { 
            //
       });

I want to use some variable name in place of (.icon a) eg

我想用一些变量名来代替(.icon a)例如

        var name=anyvalue;
        $(name).hover(function(){
       //
    });

1 个解决方案

#1


0  

You can do that, or you can assign the object itself to a variable. Like so

您可以这样做,或者您可以将对象本身分配给变量。像这样

var iconName = ".icon a";
$(iconName).hover(function(){ });

or

var $icon = $(".icon a");
$icon.hover(function () { });

or even both

甚至两者

var iconName = ".icon a";
var $icon = $(iconName);
$icon.hover(function () { });

#1


0  

You can do that, or you can assign the object itself to a variable. Like so

您可以这样做,或者您可以将对象本身分配给变量。像这样

var iconName = ".icon a";
$(iconName).hover(function(){ });

or

var $icon = $(".icon a");
$icon.hover(function () { });

or even both

甚至两者

var iconName = ".icon a";
var $icon = $(iconName);
$icon.hover(function () { });