2011-02-09 88 views
1

我有這個填充到目前爲止...遍歷動態標籤和使用jquery

<script type="text/javascript"> 
    $(document).ready(function() { 
     $('ul').addClass('navMenu'); 
     $('li').addClass('todo').html("<span class='navItemHead'></span>"); 
     $('span.navItemHead').wrap("<a href='/My/Website'></a>"); 
    }); 
</script> 

我有四個名字,我想投入名稱標籤瑪麗,尼克,馬特,梅格和1,2, 3,4的跨度標籤,所以最終的HTML應該讀.....

<li class="todo "> 
     <a href="/Register/Website"><span class="navItemHead">1</span>Mary</a>  
</li> 
<li class="todo "> 
     <a href="/Register/Website"><span class="navItemHead">2</span>Nick</a> 
</li> ....etc 

回答

1
var names = ['Mary', ...]; 
$('li').addClass('todo').html(function (i) { 
    return "<a href='/My/Website'><span class='navItemHead'>" + (i + 1) + "</span>" + names[i] + "</a>"; 
});