Javascript jquery - 从函数返回一个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6019859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery - returning a value from function
提问by Michel Joanisse
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 callon 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
提前致谢!非常感谢所有建议/意见。
干杯
回答by Jacob Mattison
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
.
如果您知道永远只有一个活动,您可以回到原来的方法,只需return activePane
在console.log
.
回答by lsuarez
Forget everyone who says return activePane
since they didn't see it's in a jQuery each
loop. Won't work.
忘记那些说return activePane
因为他们没有看到它在 jQueryeach
循环中的人。不会工作。
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:
我建议重组你的选择器。您应该使用的选择器是:$("#slider .box .panel:visible")
。这将完全消除您的每个循环。例如,您可以按如下方式重构代码:
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.
我建议只使用内嵌的选择器而不是创建一个新函数,但这是一个品味问题,尤其是如果您必须在多个地方选择相同的东西。
回答by Nicola Peluchetti
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.
我认为这就是你所需要的。
回答by Piotr Kula
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.
但是请确保您返回字符串 - 或者如果您想将它作为文本显示在某处,则转换为字符串。
回答by n00dle
Just switch your console line to a return statement:
只需将您的控制台行切换到 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();
To call:
致电:
function exampleCase() {
var thepane = checkPanes(); //evidently, does not return anything.
// ...
}
回答by pixelbobby
I think it's as easy as using return activePane;
我认为这就像使用一样简单 return activePane;
回答by ADW
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);
}
}