ASP.NET MVC / Jquery Unobtrusive Ajax,尝试在返回html数据后检查元素是否用Jquery加载
将 jquery 非侵入式 ajax 库与 ASP.NET MVC3 结合使用,单击 ajax 操作链接后,当服务器返回响应(html 数据)时,它会自动放入指定的目标 #id 中。就我而言,该数据还包含浏览器随后将获取图像的元素。我想要做的就是一旦返回此数据,就将其隐藏起来,直到加载所有图像,同时显示加载 div。默认的loadingelementid和duration ajax选项在我的情况下不会削减它,因为它不会检查图像是否已加载并且替换其内容的div包含ajax.actionlink。问题是当尝试挂钩 OnBegin、OnSuccess 等事件以获取 ajax 响应时,由于某种原因,$.load 不会被执行。
.ColumnContainer
位于 #BodyContent
内部,包含需要加载的图像。
<a data-ajax="true" data-ajax-mode="replace" data-ajax-begin="OnBegin" data-ajax-success="OnSuccess" data-ajax-update="#ContentBody"
data-ajax-url="/Store/StoreContent/@Model.category/@(Model.PagingInfo.CurrentPage - 1)" href="/@Model.category/page/@(Model.PagingInfo.CurrentPage - 1)"> <img src="/Content/extremepc/leftarrow.png" /></a>
function OnBegin() {
$('.ColumnContainer').hide();
$('#LoadingLayer').css('display', 'inline-block');}
function OnSuccess(data) {
$('.ColumnContainer').hide();
$('#LoadingLayer').css('display', 'inline-block');
FinishIt();
}
function FinishIt() {
$('.ColumnContainer').load(function () { alert('This code does not execute :( '); });
$('#LoadingLayer').css('display', 'none');
}
Using the jquery unobtrusive ajax library with ASP.NET MVC3, after clicking the ajax actionlink and when the response (html data) from the server is returned it automatically gets put into the designated target #id. In my case this data also carries elements for which the browser will then get the images. All I want to do is once this data is returned is to keep it hidden until all images are loaded, displaying a loading div in the meantime. The default loadingelementid and duration ajax options won't cut it in my case since it doesn't check if images have loaded and the div that is having its contents replaced contains the ajax.actionlink's. The problem is no $.load is executed for some reason when trying hook into the OnBegin, OnSuccess etc. events for the ajax response.
.ColumnContainer
is inside #BodyContent
and contains the images that need to be loaded.
<a data-ajax="true" data-ajax-mode="replace" data-ajax-begin="OnBegin" data-ajax-success="OnSuccess" data-ajax-update="#ContentBody"
data-ajax-url="/Store/StoreContent/@Model.category/@(Model.PagingInfo.CurrentPage - 1)" href="/@Model.category/page/@(Model.PagingInfo.CurrentPage - 1)"> <img src="/Content/extremepc/leftarrow.png" /></a>
function OnBegin() {
$('.ColumnContainer').hide();
$('#LoadingLayer').css('display', 'inline-block');}
function OnSuccess(data) {
$('.ColumnContainer').hide();
$('#LoadingLayer').css('display', 'inline-block');
FinishIt();
}
function FinishIt() {
$('.ColumnContainer').load(function () { alert('This code does not execute :( '); });
$('#LoadingLayer').css('display', 'none');
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 waitForImages 插件:
您将有一个 DOM 元素来容纳新的 DOM:
并在您的AjaxOptions 您只需指定
OnSuccess
事件,您不应该使用UpdateTargetId
因为它会立即将新内容注入到 DOM 中,而无需等待图像已加载:You could use the waitForImages plugin:
Where you would have a DOM element to harbor the new DOM:
and in your AjaxOptions you would specify the
OnSuccess
event only, you should not useUpdateTargetId
as it will immediately inject the new contents into the DOM without waiting for images to be loaded:我不太确定你在做什么,但我认为这里有一些错误,应该修复这些问题,以更好地帮助我们理解你的意图:
I'm not quite sure what you are doing, but there are a couple of things I see wrong here that should be fixed to better help us understand your intent: