1. 程式人生 > >jQuery 用each() 新增click事件

jQuery 用each() 新增click事件

程式碼如下:

mydd = $('.plist');

mydd.each(function(i){

     $(this).click(function(){

           //mydl.eq(i).hide("slow");      

            mydd.eq(i).css("background","url(images/m_1281702690741.gif) -1px center no-repeat;");

      })

  });

注意:此處 this 指代的是 DOM 物件而非 jQuery 物件。
如果遍歷的是圖片,你就可以用以下的方式:
this.src = “test” + i + “.jpg”;