2011-11-17 51 views
0

我刚开始使用jQuery,并且遇到了一些其他语言可以轻松完成的操作。JQuery:显示关于包含在列表中的唯一元素的信息

我有一个元素列表,每个元素包含一个信息列表。 我的问题来自于我无法使用jQuery来关于这些元素的slideDown或slideUp信息,因为我无法获得每个元素的唯一标识符。

$(function() 
{ 
    classe = "#afficheDetails" + 0; 
    $(classe).click(function() 
    { 
     affiche = "#detailsFeuille" + 0; 
     if($(affiche).css("display") == "block") 
     { 
      $(affiche).slideUp(); 
     } 
     else 
     { 
      $(affiche).slideDown(); 
     } 
    }); 
}); 

更新:

而且,我在一个for循环,所以我并不总是相同数量的元素,以显示...

<p id='afficheDetails<?php echo $i;?>'>to display info, click here</p> 

      <table id='detailsFeuille<?php echo $i;?>'> 
       <tr> 
        ... 
       </tr> 

       <tr> 
        ... 
       </tr> 
</table> 

这种事情是实际上工作,但你可以看到我没有动态添加指令,这都是手动的。我的elem列表可以包含很多元素。

我试图创建一个非匿名函数,但每次我调用它时,都会出现一个错误:myFunction未定义。

你能帮我吗?

Thx!

+0

会有很大帮助以查看标记。 – Filip

回答

2

通过指数

// Select all elements that start with "afficheDetails" 
$("[id^='afficheDetails']").click(function(){ 
    var index = $("[class^='afficheDetails'").index(this); // Index of the element 
    $("[id^='detailsFeuille']").eq(index).slideToggle(); 
}); 

检查这个演示:http://jsfiddle.net/fRyjV/1/

通过ID

$("[id^='afficheDetails']").click(function(){ 
    // Replace the first part of the ID with nothing to get the integer. 
    var id = this.id.replace("afficheDetails", ""); 
    $("#detailsFeuille" + id).slideToggle(); 
}); 

检查这个演示http://jsfiddle.net/fRyjV/

+0

你的代码实际上并没有工作,但我明白了,我认为它会非常有帮助,谢谢! – beluga

+0

他们俩都应该现在工作,我在我的选择器中错过了1''''。查看演示答案。 – Niels

+0

帅哥...你真棒!非常感谢你,这和我的预期完全一样!再次感谢 ! – beluga

相关问题