2013-02-02 30 views
-1

不工作jQuery的負載之後,我使用兩個jQuery的通過jQuery

首先點擊「第一」,顯示內容

後點擊「兩次」,但不顯示內容

此鏈接,我的代碼:http://jsfiddle.net/cFxsa/

代碼:

$(function(){ 
    $('.first').click(function(){ 
     $('#load').html('<a href="#" class="second">Twice</a>'); 
    }); 

    $('.second').click(function(){ 
     $('#twice').html('<a href="#" class="third">Third</a>'); 
    }); 
}); 
+0

此刻你要綁定的事件處理程序'$(「第二個」)',沒有這樣的元素還不存在。 –

回答

3

click可只附着exsiting元素,.second你試圖添加一個回調之後被加入,on支持delegation events

$('.first').click(function(){ 
    $('#load').html('<a href="#" class="second">Twice</a>'); 
}); 

$('#containerId').on('click', '.second', function(){ 
    $('#twice').html('<a href="#" class="third">Third</a>'); 
}); 

Fixed Fiddle

0
$(".first").click(function() { 
    $("#load").html('<a href="#" class="second">Twice</a>'); 
}); 
$(document).delegate(".second","click",function() { 
    $("#twice").html('<a href="#" class="third">Third</a>'); 
}); 
0

Reasign了。點擊()以$( '.second')後面的$('。first')。click()因爲.html()插入了新的東西。 爲了方便使用的功能,如:

功能firstClick(){ - 你的代碼 } 功能secondClick(){ - 你的代碼 }

可以更容易reasign..just通話'secondClick();'

希望這將有助於.. :)