Javascript jquery-从函数返回值
假设我有以下功能:Javascript jquery-从函数返回值,javascript,jquery,function,return-value,javascript-framework,Javascript,Jquery,Function,Return Value,Javascript Framework,假设我有以下功能: 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.lo
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();
理想情况下,我希望在其他地方调用此函数(很可能是从另一个函数调用),
并检索当前输出到控制台的值
(例如……)
提前谢谢!非常感谢所有建议/意见。欢呼声注意到了这个循环;看起来您可能希望返回的是所有活动面板的数组(因为理论上可能不止一个)
如果您知道只有一个活动,您可以回到原来的方法,只需在
控制台后添加返回活动窗格
;看起来您可能希望返回的是所有活动面板的数组(因为理论上可能不止一个)
如果您知道只有一个活动,您可以回到原来的方法,只需在控制台.log
之后添加返回活动窗格
,只需将控制台行切换到返回语句:
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.
// ...
}
只需将控制台行切换到返回语句:
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.
// ...
}
我认为这就像使用returnactivepane一样简单代码>我认为它和使用返回活动窗格一样简单代码>此:
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);
}这是:
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);
}忘记所有说返回activePane
的人,因为他们没有看到它在jQuery的每个循环中。不行
我建议重组你的选择器。您应该使用的选择器是:$(“#slider.box.panel:可见”)
。这将完全切断您的每个循环。例如,您可以按如下方式重新构造代码:
function checkPanes() {
return $("#slider .box .panel:visible").index();
}
function exampleCase() {
var visiblePane = checkPanes();
// ... do something with the index
}
我建议直接使用选择器,而不是创建一个新函数,但这是一个品味的问题,特别是如果你必须在多个地方选择相同的东西。忘记所有说返回activePane
的人,因为他们没有看到它在jQuery每个
循环中。不行
我建议重组你的选择器。您应该使用的选择器是:$(“#slider.box.panel:可见”)
。这将完全切断您的每个循环。例如,您可以按如下方式重新构造代码:
function checkPanes() {
return $("#slider .box .panel:visible").index();
}
function exampleCase() {
var visiblePane = checkPanes();
// ... do something with the index
}
我建议直接使用选择器,而不是创建一个新函数,但这是一个品味问题,尤其是当您必须在多个位置选择同一个对象时。您必须在第一个函数中返回某个对象,才能在第二个函数中操作它:
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
}
我认为这就是您需要的。您必须在第一个函数中返回某些内容,才能在第二个函数中对其进行操作:
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
}
我认为这就是您需要的。如果您想在其他地方使用这些值,您可以保留代码并添加一个返回
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!
}
但是要确保返回字符串,或者如果您想在某个地方将其禁用为文本,请将其转换为字符串。您可以保留代码,如果您想在其他地方使用这些值,只需添加一个返回
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!
}
但要确保返回字符串,或者如果您想在某个地方将其禁用为文本,请将其转换为字符串。您是否尝试过returnactivepane代码>?您是否尝试过返回activePane代码>?我想是的。。(我对所有这些都是相当陌生的)-那么,我的下一个问题是,如何登录到控制台,该函数的“return”值?或者在其他地方使用该返回值?(可能根据返回的值创建一个新变量)-干杯!如果在另一个函数中执行类似于var-activePanes=checkPanes()的操作
您将把返回值放入局部变量中,并可以随意使用它(或记录它)。我想是的。。(我对所有这些都是相当陌生的)-那么,我的下一个问题是,如何登录到控制台,该函数的“return”值?或者在其他地方使用该返回值?(可能根据返回的值创建一个新变量)-干杯!如果在另一个函数中执行类似于var-activePanes=checkPanes()的操作
您将把返回值放入局部变量中,并可以随意使用(或记录)。它可能在循环中,但由于注释表明我们正在寻找处于可见状态的/one/窗格,一旦找到它,我们就可以退出循环。这是我试图实现的简化版本。。谢谢对哼!这么简单。。。我在其他任何地方都找不到这个简单的答案。。。简短回答:如果您将函数的结构类似于var,比如var data=get_data()
,而不是get_data()
,那么您可以使用在同一个函数中获得的数据。它可能处于循环中,但由于注释表明我们正在查找处于可见状态的/one/窗格,一旦找到它,我们可以退出循环。这是我试图实现的一个简化版本。。谢谢对哼!这么简单。。。我在其他任何地方都找不到这个简单的答案。。。简短回答:如果您将函数的结构类似于var,比如var data=get_data()
而不是get_data()
,那么您可以使用在同一个函数中获得的数据。啊,好的,所以您必须先将函数传递给一个变量,然后再检索它的值以便登录到控制台等。谢谢!你不必总是这样。但是,大多数其他答案的原始函数都会提前返回,这可能非常糟糕(在这种情况下几乎肯定不会,但可能是现在或将来)。我所拥有的比大多数其他变量都“更安全”,这就是我使用该变量的原因。啊,好的,所以你必须先将函数传递给一个变量,然后再检索它的值,以便登录到控制台,等等。谢谢!你不必总是这样。但是,大多数其他答案的原始函数都会提前返回,这可能非常糟糕(几乎是错误的)