【发布时间】:2018-06-16 05:46:19
【问题描述】:
我的功能有问题。我知道返回我想要的变量的行已经到达,但由于某种原因它没有返回。这是我的功能:
console.log(getLabel(mySelectElementNode)); // this is undefined
function getLabel(element) {
// This function tries to find the label associated with the select element
// select.labels is the usual property, but not all browsers support it
//get the sibling nodes of the label we need
var siblings = element.parentNode.parentNode.childNodes;
siblings.forEach(function (elem,ind) {
if (elem.nodeName=='LABEL') {
console.log(elem); //I can see this in my browser and it is the label I am looking for
return elem; // but this return statement does nothing
};
});
console.log('label not found');
}
我不明白为什么我的退货声明不起作用。我也试过把 return null;在 console.log 语句之后的函数末尾,但即使这样也没有用。
【问题讨论】:
-
你是从匿名函数内部返回的,而不是外部函数。
-
你想要什么回报?结束迭代还是返回一个值以进一步使用它?
标签: javascript html return