0

这可能是一个非常简单的修复。我似乎无法弄清楚这一点。

我已将事件名称存储在变量中,并希望在代码中使用它们,如下面的伪代码所示,但它似乎对我不起作用。请参阅下面代码中的注释。

var EVENT;
if (something == true) EVENT = "hover";
else EVENT = "click";

mynav = function() {
   $(".nav").not(".nav2").bind(EVENT, function() { // EVENT works fine here
   ......
   ......
   ......
   } (EVENT == "hover")?', function() {}':''; ); // If EVENT is "hover", I want to display function()... Not sure how to make it work here....
}
4

3 回答 3

0
var EVENT = something == true ? "hover" : "click";
mynav = function() {
    $(".nav").not(".nav2").bind(EVENT, EVENT === "hover" ? function() {
        // Hover function
    } : function() {
        // Click function
    });
}
于 2013-03-17T15:58:03.697 回答
0

尝试像这样分开你的函数:

function myHover () {}

function myClick() {}

并在您的代码中调用您的函数:

mynav = function() {
   $(".nav").not(".nav2").bind(EVENT, function() { // EVENT works fine here
   ......
   ......
   ......
   } , ((EVENT == "hover")?:myHover : myClick ));
}
于 2013-03-17T15:55:01.403 回答
0

创建函数然后在任何你想要的地方调用它

function MyHover(){
alert('MyHover()');
} 

function MyClick(){
alert('MyClick()');
} 

var EVENT;
if (something == true) EVENT = "hover";
else EVENT = "click";

mynav = function() {
   $(".nav").not(".nav2").bind(EVENT, function() { // EVENT works fine here
   ......
   ......
   ......
   } (EVENT == "hover")? MyHover() : MyClick() ; ); // If EVENT is "hover", I want to display function()... Not sure how to make it work here....
}
于 2013-03-17T15:31:43.567 回答