2012-03-29 53 views
0

我有这段代码来识别两个不同的图像映射形状属性。我试图(不成功)做的是让两个不同的形状从数组的开头索引标题属性。如何获得第二个请求从数组索引开始与jQuery开始?

代码:

window.slideDetails = [ 

     //Slide1  
     "C1BP7: user information", 
     "FJAD7: user information", 
     "FFAD7: user information", 
     //Slide2 
     "C0AE7: user information", 
     "C7AZ7: user information", 
     "FJAE7: user information", 
     //Slide3 
     "C1AW7: user information", 
     "FJAP7: user information", 
     "FFAD7: user information" 

     ]; 
     $('area').each(function(index) { 
      var shape=$(this).attr('shape'); 
       if (shape == 'rect') { 
        $(this).attr("title", window.slideDetails[index]); 
       } 

       else if (shape == 'poly') { 
        $(this).attr("title", window.slideDetails[index]); 
       } 

    }); 

它的索引,以便他们因此前三“聚”属性得到数组中的前三个,那么接下来的三个属性,其是“矩形”获得4-6等等。我对索引&数组了解不够,我很确定我正在使用索引。

HTML

<img src="/images/testImage.jpg" width="1000" height="610" border="0" usemap="#Map1" /> 
<map name="Map1" id="Map1"> 
    <area shape="poly" coords="280,140,296,201,308,202,328,183,324,141,340,126,360,134,359,254,332,252,333,214,324,274,327,407,202,392,201,330,223,331,219,291,231,243,210,238,232,131,252,118" href="test1.htm" /> 
    <area shape="poly" coords="277,113,296,199,308,197,326,183,323,115,316,138,293,138" href="test2.htm" /> 
    <area shape="poly" coords="212,507,218,571,236,543,238,575,261,587,270,586,262,539,245,537,237,544,234,537,236,520,228,496,214,497" href="test3.htm" /> 
    <area shape="rect" coords="609,235,834,335" href="test1.htm" /> 
    <area shape="rect" coords="649,546,807,565" href="test2.htm" /> 
    <area shape="rect" coords="670,566,781,582" href="test3.htm" /> 
</map> 

的矩形和聚形状的图像的不同部分,而是先各自的走同样的链接等

+0

你可以出示你的标记吗? :0 – 2012-03-29 08:33:51

+0

标记添加andreas。 – JayDee 2012-03-29 08:38:31

+0

期望的输出是什么? – 2012-03-29 08:46:25

回答

1

的问题是,你正在做一个循环,指定每个区域的索引,而不是每个形状的索引。

相反,你可以使用两个循环(因此指数被重新设置了不同类型的形状),并包括形状属性为您selector的一部分:

$('area[shape="rect"]') 
    .each(function(index) { 
     $(this).attr("title", window.slideDetails[index]); 
    }); 

$('area[shape="poly"]') 
    .each(function(index) { 
     $(this).attr("title", window.slideDetails[index]); 
    }); 

看到这个小提琴:http://jsfiddle.net/mKc6T/

+0

我想我会搞砸索引函数,谢谢你清楚地解释它,并提供一个代码来展示我出错的地方,它完美地工作,非常感谢你:) – JayDee 2012-03-29 09:51:54

相关问题