$('body').on('click', function() {
$('.detailTextContainer a').each(function() {
var urlFoto = $(this).attr('href');
var imgElement = $("<img />").attr('src', urlFoto)
.addClass('dalsiFoto');
$(this).empty().append(imgElement);
});
});
如何在加载页面(无事件)后自动运行此函数。 '.detailTextContainer a'
是动态元素,它们在启动页面后立即加载到页面。jQuery动态元素自动运行的每种方法
我试过这段代码:
function showImage(){
$('.detailFeatureDesc a').each(function() {
var urlFoto = $(this).attr('href');
var imgElement = $("<img />").attr('src', urlFoto)
.addClass('dalsiFoto');
$(this).empty().append(imgElement);
});
};
showImage();
但是,这并没有成功。
感谢
参见:http://api.jquery.com/on/ 和https://stackoverflow.com/questions/1359018/in-jquery-how-to-attach-events-to-dynamic-html-elements –
@Kamil请分享您的HTML代码或锚定标记代码,它们是动态加载的。 – Shiladitya