2010-02-16 2 views

Répondre

3

Si elles sont garanties à apparaître de façon séquentielle dans votre document, vous pouvez faire:

<li id="name1">Name1</li> 
<li id="name2">Name2</li> 

alert($("li[id^=name]:last").attr("id")); 

ou si vous avez juste besoin le nombre :

alert($("li[id^=name]").length); 

[id^=name] signifie que l'attribut id starts with 'nom'.

2
function getLiIdWithMaxValue() 
{ 
    var liIdMaxValue = 0; 
    var lisWithName = $('LI[id^=name]'); //Use of @Karims79 terse selector 
    for(var i = 0; i < lisWithName.length; i++) { 
     var idNo = parseInt($(lisWithName[i]).attr('id')); 
     if (idNo > liWithMaxName) 
     { 
     liIdMaxValue = idNo; 
     } 
    } 
    return liIdMaxValue;  
} 
Questions connexes