.on
无法在我的页面上将单击事件应用于使用ajax加载的元素。#myimageflow
是容器,我使用ajax加载图像。当我为点击尝试.on时,它不工作,相反,.live工作得很好。
$(document).ready(function() {
/* Reading the data from XML file*/
$.ajax({
type: "GET",
url: "photos.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('item').each(function() {
var path = $(this).attr('path');
var width = $(this).attr('width');
var height = $(this).attr('height');
var id = $(this).attr('id');
var alt = $(this).attr('alt');
var longdesc = $(this).find('longdesc').text();
var description = $(this).find('desc').text();
$('#myImageFlow').append('<img src="' + path + '" id=' + id + ' height="' + height + '" width="' + width + '" longdesc="' + longdesc + '" alt="' + alt + '"/>');
imgArr[i] = description;
imgFront[i] = longdesc;
i = i + 1;
});
}
});
/* ===================================== */
//$("#myImageFlow").show();
$.getScript('js/iSlider.js');
$.getScript('js/code.photoswipe.jquery-3.0.5.js');
});
/*front div called when front or back is called*/
$('#myImageFlow').on("click", 'img', function() {
alert("image clicked");
});
发布于 2013-05-27 20:06:51
将此部分放入ready
回调中:
$('#myImageFlow').on("click", 'img', function() {
alert("image clicked");
});
执行此绑定时,必须找到#myImageFlow
元素。当您将此代码放在ready
回调函数之外时,它将在元素可用之前执行。
发布于 2013-05-27 20:14:23
通过制造它来解决,
$(document).on('click', 'img', function() {
alert('htmlcalled image Clicked Called='+clickEnabled);
......
}
发布于 2013-05-27 20:06:29
.live
is deprecated.If您正在使用旧版本的jQuery .live()
将为您工作。.on
将不起作用
https://stackoverflow.com/questions/16772831
复制相似问题