2012-12-13 36 views
0

我有一种情况,即生成动态列表。现在我试图点击列表中的每个项目并显示每个项目的内容。我知道我可以使用另一个阵列,我可以将内容存储为div,然后指定要显示的位置。只是不知道如何写它。这是我到目前为止单击动态列表创建的项目并显示动态内容

<ul id="list"> 
<!--- List items will be added dynamically. ---> 
</ul> 

$(InitPage); 

    function InitPage() { 

     var jList = $("#list"); 

     var arrValues = ['<div id="one"></div>', '<div id="two"></div>', '<div id="three"></div>']; 

     $.each(
     arrValues, 

     function (intIndex, objValue) { 

      jList.append(
      $('<li class="thumbItem">' + objValue + "</li>")); 
     }); 
    } 

css-- 
li.thumbItem{ 
     float:left; 
     width:192px; 
     height:192px; 
     background:gray; 
     margin:10px 10px 0 0; 
     cursor:pointer; 
    } 

回答

3

为什么不使用委派?

$(function(){  
    $('#list').on('click','li',function(){ 
     //do stuff here 
    }); 
}); 

的jQuery 1.4,使用.live():

$('#list li').live('click', function(){ 
      //do stuff here 
     }); 
+0

这可以工作,但问题是我使用jquery 1.4.2 ...所以基本上当你点击每个项目里他们应该有一个相应的内容,可能work.i试过这一个...并没有..可能是因为jquery版本不支持它 $('。contentHolderContent')。eq($(this).index()) 。显示(); – soum

+0

“因为jquery版本不支持它”jquery 1.7+支持Ya,true,.on(),而是使用.live()。查看我的更新。 –

+0

dint没有真正的工作...这是我在做什么 <! - 这是生成列表生活的地方 - > 生成列表 <! - 这是生成的列表生活的地方 - > <! - -contents上显示点击 - >

<! - 点击脚本 - > $( '#contentHolder')儿童()ATTR( '类', 'contentHolderContent') ; \t $( '#名单礼')生活( '点击',函数(){ $( 'li.thumbItem')removeClass( '活动'); \t \t \t $(本).addClass( ''active;); \t \t \t $('。contentHolderContent')。eq($(this).index())。show(); }); – soum