我想将此功能应用于由ajax加载的内容。问题是,该功能不适用于此类内容。这个每个功能有某种解决方法吗?
$(".liquidcam").each(function(index) {
if( (!this.width || !this.height) ) {
$(this).parent().parent().hide("fast");
}
$(this).error(function() {
$(this).parent().parent().hide("fast");
});
});
我最近遇到了同样的问题。大多数插件需要通过调用它们来应用到元素$(document).ready()
,我不仅想为文档加载后的内容调用相同的函数,还想为稍后通过 Ajax 调用到达的那些元素调用相同的函数。
经过谷歌搜索一段时间后,我发现了两种类型的推荐:
1. 绑定到每个ajax调用的最终事件。
2. 绑定到某些元素特定事件(例如focus()
)来运行您的代码。
在第一种情况下,您需要在 ajax 调用中注意它。
第二种方法并不适用于所有情况。
所以我的解决方案是调用相同的函数$(document).ready()
and $(document).ajaxSuccess()
。在这种情况下,我需要防止同一个控件发生两次相同的操作。所以我使用了一个特殊的类,我将其应用于所有访问过的元素。
function doOnReady() {
$(":input[data-autocomplete]").each(function () {
//Check wether allready visited
if (!$(this).hasClass('visited-by-on-ready')) {
//Do your staff
$(this).autocomplete({
source: $(this).attr("data-autocomplete"),
});
//mark as visited
$(this).addClass('visited-by-on-ready');
}
});
}
$(document).ready(doOnReady);
$(document).ajaxComplete(doOnReady);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)