假设我具有以下功能:
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.
} 提前感谢!所有的建议/意见都非常感谢。
干杯
发布于 2011-05-16 15:52:49
忘记那些说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
}我建议只使用线上的选择器,而不是做一个新的功能,但这是一个口味的问题,特别是如果你必须选择相同的东西在多个地方。
发布于 2011-05-16 15:48:20
刚刚注意到了循环;看起来您可能希望返回的是所有活动面板的数组(因为理论上可能有多个面板)。
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;
} 如果您知道只有一个active,您可以回到原来的方法,只需在return activePane之后添加console.log。
发布于 2011-05-16 15:49: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; // Return the value and leave the function
}
});
} //END checkPanes();打电话:
function exampleCase() {
var thepane = checkPanes(); //evidently, does not return anything.
// ...
} https://stackoverflow.com/questions/6019859
复制相似问题