我正在通過javascript向我的文檔添加一些HTML。我也通過這個javascript函數爲我的HTML添加一個按鈕。我想要這個按鈕有一個事件監聽器,但是這似乎不適用於用javascript添加的元素。有沒有可能解決這個問題?如何獲得一個事件偵聽器來處理通過javascript添加到我的html中的元素?
我的JS:
$('#content-overview li').on('click',function(){
// Clear previous videos
$('.content-overview-row').remove();
// Insert new video
var vimeoId = $(this).data('vimeo');
var html = '<li class="content-overview-row"><div class="content-overview-video"><iframe src="http://player.vimeo.com/video/'+vimeoId+'" width="950" height="534"></iframe><a id="close-video"></a></div></li>';
// Find end of row and insert detailed view
var nextRow = $(this).nextAll('.first-in-row:first');
if(nextRow.is('li')){
nextRow.before(html);
}
else{
//last row
if($(this).hasClass('first-in-row'))
{
//first item clicked in last row
$(this).before(html);
}
else{
$(this).prevAll('.first-in-row:first').before(html);
}
}
return false;
$('#close-video').click(function() {
console.log("works");
});
});
在調用'.click()' – zeroflagL 2013-04-26 07:25:28