jquery - 从函数返回一个值

发布于 2024-11-07 09:18:05 字数 655 浏览 0 评论 0原文

假设我有以下函数:

function checkPanes() {
    activePane = '';
    var panels = $("#slider .box .panel");

    panels.each(function() {

    //find the one in visible state.
    if ($(this).is(":visible")) {
    activePane = $(this).index()+1;
    console.log(activePane);
    }

    });
} //END checkPanes();

理想情况下,我想在其他地方调用此函数(很可能是从另一个函数), 并检索我当前输出到控制台的值。

(例如..)

function exampleCase() {
    checkPanes(); //evidently, does not return anything. 
    //Ideally, I want the numerical value, being output to console in above function.
}  

提前致谢!非常感谢所有建议/意见。
干杯

say I have the following function:

function checkPanes() {
    activePane = '';
    var panels = $("#slider .box .panel");

    panels.each(function() {

    //find the one in visible state.
    if ($(this).is(":visible")) {
    activePane = $(this).index()+1;
    console.log(activePane);
    }

    });
} //END checkPanes();

Ideally, I'd like to call on this function elsewhere (most likely from another function),
and retrieve the value I am currently outputting to console.

(example ..)

function exampleCase() {
    checkPanes(); //evidently, does not return anything. 
    //Ideally, I want the numerical value, being output to console in above function.
}  

Thanks in advance! All suggestions / comments are well appreciated.
Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

是你 2024-11-14 09:18:05

刚刚注意到循环;看起来您可能想要返回的是所有活动面板的数组(因为理论上可能有多个面板)。

function checkPanes() {
    activePanes = [];
    var panels = $("#slider .box .panel");

    panels.each(function() {

    //find the one in visible state.
    if ($(this).is(":visible")) {
    activePane.push($(this).index()+1);
    console.log(activePane);
    }

    });
    return activePanes;
} 

如果您知道只有一个处于活动状态,则可以返回到原来的方法,只需在 console.log 之后添加 return activePane 即可。

Just noticed the loop; looks like what you may want to return is an array of all active panels (since in theory there could be more than one).

function checkPanes() {
    activePanes = [];
    var panels = $("#slider .box .panel");

    panels.each(function() {

    //find the one in visible state.
    if ($(this).is(":visible")) {
    activePane.push($(this).index()+1);
    console.log(activePane);
    }

    });
    return activePanes;
} 

If you know there will only ever be one active, you can go back to your original approach and just add return activePane after the console.log.

夏雨凉 2024-11-14 09:18:05

忘记那些说 return activePane 的人吧,因为他们没有看到它位于 jQuery each 循环中。行不通。

我建议重组你的选择器。您应该使用的选择器是:$("#slider .box .panel:visible")。这将完全切断你的每个循环。例如,您可以按如下方式重组代码:

function checkPanes() {
    return $("#slider .box .panel:visible").index();
}

function exampleCase() {
    var visiblePane = checkPanes();

    // ... do something with the index
}

我建议仅使用内联选择器而不是创建新函数,但这是一个品味问题,特别是当您必须在多个位置选择相同的内容时。

Forget everyone who says return activePane since they didn't see it's in a jQuery each loop. Won't work.

I'd suggest restructuring your selector. The selector you should be using is: $("#slider .box .panel:visible"). This will cut out your each loop entirely. For instance you could restructure the code as follows:

function checkPanes() {
    return $("#slider .box .panel:visible").index();
}

function exampleCase() {
    var visiblePane = checkPanes();

    // ... do something with the index
}

I'd suggest just using the selector in-line rather than making a new function, but that's a matter of taste, especially if you have to select the same thing in multiple places.

心碎无痕… 2024-11-14 09:18:05

只需将控制台线切换到 return 语句即可:

function checkPanes() {
    activePane = '';
    var panels = $("#slider .box .panel");

    panels.each(function() {

    //find the one in visible state.
    if ($(this).is(":visible")) {
        activePane = $(this).index()+1;
        return activePane; // Return the value and leave the function
    }

    });
} //END checkPanes();

调用:

function exampleCase() {
    var thepane = checkPanes(); //evidently, does not return anything. 
    // ...
}  

Just switch your console line to a return statement:

function checkPanes() {
    activePane = '';
    var panels = $("#slider .box .panel");

    panels.each(function() {

    //find the one in visible state.
    if ($(this).is(":visible")) {
        activePane = $(this).index()+1;
        return activePane; // Return the value and leave the function
    }

    });
} //END checkPanes();

To call:

function exampleCase() {
    var thepane = checkPanes(); //evidently, does not return anything. 
    // ...
}  
棒棒糖 2024-11-14 09:18:05

我认为这就像使用 return activePane; 一样简单

I think it's as easy as using return activePane;

尸血腥色 2024-11-14 09:18:05

这个:

function checkPanes() {
  activePane = '';
  var panels = $("#slider .box .panel");

  panels.each(function() {

  //find the one in visible state.
  if ($(this).is(":visible")) {
  activePane = $(this).index()+1;
  }

  });
  return activePane;
} //END checkPanes();

还有这个:

function exampleCase() {
   var myval=checkPanes(); //evidently, does not return anything. 
   console.log(myval);

}

This:

function checkPanes() {
  activePane = '';
  var panels = $("#slider .box .panel");

  panels.each(function() {

  //find the one in visible state.
  if ($(this).is(":visible")) {
  activePane = $(this).index()+1;
  }

  });
  return activePane;
} //END checkPanes();

and this:

function exampleCase() {
   var myval=checkPanes(); //evidently, does not return anything. 
   console.log(myval);

}

戏舞 2024-11-14 09:18:05

如果您想在其他地方使用这些值,您可以保留代码并添加返回值,

function checkPanes() {
 activePane = '';
 var panels = $("#slider .box .panel");

  panels.each(function() {

  //find the one in visible state.
  if ($(this).is(":visible")) {
  activePane = $(this).index()+1;
  console.log(activePane); //Logs to console.
  return activePane; //Returns value also.
}

});
} 

因此在这里您可以使用返回值或仅将其记录到控制台。这就是我理解你的问题的方式

function exampleCase() {
    checkPanes(); //Now it will still write in console. but you dont need to use the return

    alert(checkpanes()); //Would write it to console and to an alert!
} 

,但请确保返回字符串 - 或者如果你想将其作为文本显示在某处,则转换为字符串。

You can keep your code and just add a return if you want to use the values somewhere else

function checkPanes() {
 activePane = '';
 var panels = $("#slider .box .panel");

  panels.each(function() {

  //find the one in visible state.
  if ($(this).is(":visible")) {
  activePane = $(this).index()+1;
  console.log(activePane); //Logs to console.
  return activePane; //Returns value also.
}

});
} 

So in here you can either use the returned value or just have it log to console. Thats how i understood your question

function exampleCase() {
    checkPanes(); //Now it will still write in console. but you dont need to use the return

    alert(checkpanes()); //Would write it to console and to an alert!
} 

But make sure you return string - or convert to string if you want to disaply it somewhere as text.

白云不回头 2024-11-14 09:18:05

你必须在第一个函数中返回一些东西才能在第二个函数中操作它:

function checkPanes() {
    activePane = '';
    var panels = $("#slider .box .panel");
    //create a return array
    visiblePanels = [];
    panels.each(function() {

    //find the one in visible state.
    if ($(this).is(":visible")) {
    activePane = $(this).index()+1;
    //add the result to the returnb array
    visiblePanels[] = activePane
    }
    });
    // return results
    return visiblePanels;
}

function exampleCase() {
    var thepane = checkPanes();
    //now it has all the visible panels that were founded in the other function
    // you can access them with thepane[0] or iterate on them

}  

我认为这就是你所需要的。

you have to return something inside the first function to manipulate it inside the second:

function checkPanes() {
    activePane = '';
    var panels = $("#slider .box .panel");
    //create a return array
    visiblePanels = [];
    panels.each(function() {

    //find the one in visible state.
    if ($(this).is(":visible")) {
    activePane = $(this).index()+1;
    //add the result to the returnb array
    visiblePanels[] = activePane
    }
    });
    // return results
    return visiblePanels;
}

function exampleCase() {
    var thepane = checkPanes();
    //now it has all the visible panels that were founded in the other function
    // you can access them with thepane[0] or iterate on them

}  

I think this is what you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文