jquery - 从函数返回一个值
假设我有以下函数:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
刚刚注意到循环;看起来您可能想要返回的是所有活动面板的数组(因为理论上可能有多个面板)。
如果您知道只有一个处于活动状态,则可以返回到原来的方法,只需在
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).
If you know there will only ever be one active, you can go back to your original approach and just add
return activePane
after theconsole.log
.忘记那些说
return activePane
的人吧,因为他们没有看到它位于 jQueryeach
循环中。行不通。我建议重组你的选择器。您应该使用的选择器是:
$("#slider .box .panel:visible")
。这将完全切断你的每个循环。例如,您可以按如下方式重组代码:我建议仅使用内联选择器而不是创建新函数,但这是一个品味问题,特别是当您必须在多个位置选择相同的内容时。
Forget everyone who says
return activePane
since they didn't see it's in a jQueryeach
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: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.
只需将控制台线切换到 return 语句即可:
调用:
Just switch your console line to a return statement:
To call:
我认为这就像使用
return activePane;
一样简单I think it's as easy as using
return activePane;
这个:
还有这个:
}
This:
and this:
}
如果您想在其他地方使用这些值,您可以保留代码并添加返回值,
因此在这里您可以使用返回值或仅将其记录到控制台。这就是我理解你的问题的方式
,但请确保返回字符串 - 或者如果你想将其作为文本显示在某处,则转换为字符串。
You can keep your code and just add a return if you want to use the values somewhere else
So in here you can either use the returned value or just have it log to console. Thats how i understood your question
But make sure you return string - or convert to string if you want to disaply it somewhere as text.
你必须在第一个函数中返回一些东西才能在第二个函数中操作它:
我认为这就是你所需要的。
you have to return something inside the first function to manipulate it inside the second:
I think this is what you need.