Jquery在每个函数中返回未定义的值
我正在使用Jquery在每个函数中返回未定义的值,jquery,Jquery,我正在使用$循环一个对象。每个函数,但我得到的是未定义的值 我的代码有什么问题 var mainPageCircle = { circles: { c1: { color: '#730000', text: 'Content' }, c2: { color: '#004f74', text: 'Consulting' },
$循环一个对象。每个函数,但我得到的是未定义的值
我的代码有什么问题
var mainPageCircle = {
circles: {
c1: {
color: '#730000',
text: 'Content'
},
c2: {
color: '#004f74',
text: 'Consulting'
},
c3: {
color: '#146c00',
text: 'Commerce'
}
},
radious: 100,
stroke: 10,
strokeColor: '#fff',
opacity: 0.7
}
$.each(mainPageCircle, function(key, value) {
var circles = value.circles,
radious = value.radious;
$.each(circles, function(index, c) {
console.log(index, c); // i am getting error; i need index should be 0,1,2 and c should be : c1,c2,c3 values
})
})
像这样的
var mainPageCircle = {
circles :{
c1:{color:'#730000',text:'Content'},
c2:{color:'#004f74',text:'Consulting'},
c3:{color:'#146c00',text:'Commerce'}
},
radious:100,
stroke:10,
strokeColor:'#fff',
opacity:0.7
};
var i = 0;
$.each(mainPageCircle.circles, function(){
console.log(i, this);
//i: current index
//this: c1, c2 etc
//use properties on this to fetch the values
//this.color for example
i++;
});
在您的示例中,您不能将key
用作索引整数,因为它将获取对象键,而不是循环中的当前索引
像这样的
var mainPageCircle = {
circles :{
c1:{color:'#730000',text:'Content'},
c2:{color:'#004f74',text:'Consulting'},
c3:{color:'#146c00',text:'Commerce'}
},
radious:100,
stroke:10,
strokeColor:'#fff',
opacity:0.7
};
var i = 0;
$.each(mainPageCircle.circles, function(){
console.log(i, this);
//i: current index
//this: c1, c2 etc
//use properties on this to fetch the values
//this.color for example
i++;
});
在您的示例中,您不能将key
用作索引整数,因为它将获取对象键,而不是循环中的当前索引
我不确定你的意图是什么,但你可能想做如下事情:
var mainPageCircle = {
circles :{
c1:{color:'#730000',text:'Content'},
c2:{color:'#004f74',text:'Consulting'},
c3:{color:'#146c00',text:'Commerce'}
},
radious:100,
stroke:10,
strokeColor:'#fff',
opacity:0.7
}
var circles = mainPageCircle.circles,
radious = mainPageCircle.radious;
$.each(circles, function (index, c) {
console.log(index,c); // c1, props, etc...
console.log(parseInt(index,10)); // 1,2,3...
});
我不确定你的意图是什么,但你可能想做如下事情:
var mainPageCircle = {
circles :{
c1:{color:'#730000',text:'Content'},
c2:{color:'#004f74',text:'Consulting'},
c3:{color:'#146c00',text:'Commerce'}
},
radious:100,
stroke:10,
strokeColor:'#fff',
opacity:0.7
}
var circles = mainPageCircle.circles,
radious = mainPageCircle.radious;
$.each(circles, function (index, c) {
console.log(index,c); // c1, props, etc...
console.log(parseInt(index,10)); // 1,2,3...
});
您的每个都针对mainPageCircle的所有项目运行,而不仅仅是其中定义的圆圈。这可能更符合您的目标:
var mainPageCircle = {
circles :{
c1:{color:'#730000',text:'Content'},
c2:{color:'#004f74',text:'Consulting'},
c3:{color:'#146c00',text:'Commerce'}
},
radious:100,
stroke:10,
strokeColor:'#fff',
opacity:0.7
}
$.each(mainPageCircle.circles , function (key,value) {
var circles = value,radious = mainPageCircle.radious;
$.each(circles, function (index,c) {
console.log(index,c);
});
});
您的每个都针对mainPageCircle的所有项目运行,而不仅仅是其中定义的圆圈。这可能更符合您的目标:
var mainPageCircle = {
circles :{
c1:{color:'#730000',text:'Content'},
c2:{color:'#004f74',text:'Consulting'},
c3:{color:'#146c00',text:'Commerce'}
},
radious:100,
stroke:10,
strokeColor:'#fff',
opacity:0.7
}
$.each(mainPageCircle.circles , function (key,value) {
var circles = value,radious = mainPageCircle.radious;
$.each(circles, function (index,c) {
console.log(index,c);
});
});
maincrop
!=mainPageCircle
-或者这只是一个转录错误?当错误发生时,circles
是否有值?您是否尝试过使用浏览器调试工具在调查对象的每个循环中放置断点?(大多数浏览器中的F12默认键,对于Firefox get Firebug),是外部循环还是内部循环导致了问题?您是否尝试过在没有内部循环的情况下运行它?maincrop
!=mainPageCircle
-或者这只是一个转录错误?当错误发生时,circles
是否有值?您是否尝试过使用浏览器调试工具在调查对象的每个循环中放置断点?(大多数浏览器中的F12默认键,对于Firefox get Firebug),是外部循环还是内部循环导致了问题?您是否尝试过在没有内部循环的情况下运行它?。每个都已经在回调中提供了索引和元素值。根据回调(indexInArray,valueOfElement)
。应该不需要使用i
/i++
跟踪您自己的代码。您应该能够执行以下操作:$.each(mainPageCircle.circles,函数(index){console.log(index,this)}
@FrançoisWahl查看这把小提琴:。正如我在帖子中所说,indexInArray
将是本例中的对象键。啊,…我明白了。我现在只在文档中看到了关于使用的注释。在索引成为键的对象集合上,每个。当我阅读你的帖子时,我根本没有想到,如果我在阅读时没有理解的话,那就太好了。很好的一个。:/1对于这一点,很抱歉弄错了。@Françoiswah Hehe没问题,在你阅读了关于循环的整个API页面之前,它是相当混乱的;-)。每个
都已在回调中提供索引和元素值。根据回调(indexInArray,valueOfElement)
。应该不需要使用i
/i++
跟踪您自己的代码。您应该能够执行以下操作:$.each(mainPageCircle.circles,函数(index){console.log(index,this)}
@FrançoisWahl查看这把小提琴:。正如我在帖子中所说,indexInArray
将是本例中的对象键。啊,…我明白了。我现在只在文档中看到了关于使用的注释。在索引成为键的对象集合上,每个。当我阅读你的帖子时,我根本没有想到,如果我在阅读时没有理解的话,那就太好了。很好的一个。:/1对于这一点,很抱歉弄错了。@Françoiswah Hehe没问题,在你阅读了关于循环的整个API页面之前,它是相当混乱的;-)