2012-11-08 30 views
0

我不知道怎么问这个问题。如果有人不明白这一点,我很抱歉。事情是,我有对象,并且我使用each function循环,当我得到子对象(内部obejct)时,我想分配回相同的each function,那么,什么是好主意?jquery - 如何将查找对象分配给相同的每个函数?

看我的问题:

myObject的:

var xploreMaps = { 
    radious:55, 
    stroke:5,strokeColor:'#fff', 
    opacity:0.8,fontSize:13,line:10, 
    cGtext:{ 
     length:5, 
     lineColor:'#579549', 
     prop:{ 
      0:{link:'catalogs .html',color:'#7a5967',text:'Catalogs', 
      subLink:{0:{link:'SEO_SMM.html',color:'#4e4b69',text:'SEO/SMM',align:'top'},1:{link:'site_analytics.html',color:'#545454',text:'Site analytics',align:'btm'}}}, 
      1:{link:'socialmedia.html',color:'#1e9ead',text:'Innovation'}, 
      2:{link:'loyalty .html',color:'#8fad34',text:'Ideation'}, 
      3:{link:'promotions .html',color:'#563b64',text:'Promotions'}, 
      4:{link:'implementations.html',color:'#2c6566',text:'Implementations', 
      subLink:{0:{link:'integrating.html',color:'#4c4a66',text:'Integrating',align:'top'},1:{link:'payment.html',color:'#948048',text:'Payment',align:'btm'}}} 
      } 
    } 
} 

var object = xploreMaps[id].prop || 'i need subLink'; 

的MyFunction:

$.each(object, function (n,d) { 

       var Color = this.color,link = this.link,text=this.text,**subLink** = this.subLink || ''; 

    // if the sublink there, i need to put back to each, and do the same stuff, what i do for it's parent object. 

       var myTimeout = setTimeout(function(){ 
        redSubCircles.push(paper.path("M"+x +" "+y) 
        .attr({stroke:brdColor}) 
        .animate({path:"M"+(x-((stroke/2)+(n*((radious*2)+stroke+line)))) +" "+y+'l'+(-line)+' 0'},1000, 
         function(){ 
          var c = paper.circle((x-(radious+stroke+line) - n*((radious*2)+stroke+line)),y,radious) 
          .attr({ 
           stroke:strokeCl, 
           'stroke-width':stroke, 
           opacity:opacity, 
           fill:Color, 
           href:link 
          }); 

          var p = paper.text((x-(radious+stroke+line) - n*((radious*2)+stroke+line)),y,text) 
          .attr({fill:strokeCl,'font-size':fSize,href:link}) 

          redSubCircles.push(c,p)//push to set;- 

         })); 
       },1000*n) 
      }) 

如果我错了,我很抱歉。请给我正确的方法。

回答

1

也许一个while循环和一个临时数组可以帮助你。

添加您的初始项目到临时数组 通话功能,同时也有临时阵列

你遇到一个子链接将其推送到你通过它迭代将临时数组每次中的项目。临时数组变大,并继续运行。

每次你完成一个项目,删除它。临时数组变得更小,并最终达到零 - 完成你的循环。

您需要while,push和splice从阵列中移除项目。

下面是这个动作一个独立的例子,和demo here

<!DOCTYPE HTML> 
<html lang="en-US"> 
<head> 
    <meta charset="UTF-8"> 
    <title></title> 
</head> 
<body> 
<h1>looper</h1> 
    <script type="text/javascript"> 
    function ObjectMaker(identifier) { 
     var that = { 
      subObj:null, 
      name: identifier, 
      getName: function() { 
      return "I am "+this.name; 
      }, 
      setSubObj: function(sub) { 
      this.subObj = sub ; 
      }  
     } 
     return that; 
    } 
    var twoAndAHalf = ObjectMaker("two and a half"); 
    var two = ObjectMaker("two"); 
    two.setSubObj(twoAndAHalf); 
    var items = [ObjectMaker("one"),two,ObjectMaker("three")]; 
    while(items.length>0) { 
     if (items[0].subObj != null) { 
      items.push(items[0].subObj); 
     } 
     alert(items[0].getName()); 
     items.splice(0,1); 
    } 
    </script> 
</body> 
</html> 
+0

非常感谢你。 – 3gwebtrain

相关问题