ASP.NET MVC / Jquery Unobtrusive Ajax,尝试在返回html数据后检查元素是否用Jquery加载

发布于 2025-01-07 15:54:31 字数 1229 浏览 0 评论 0原文

将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如果没结果 2025-01-14 15:54:31

您可以使用 waitForImages 插件:

var onSuccess = function (result) {
    $('#result').html('loading...');
    $(result).waitForImages(function () {
        $('#result').html(this);
    });
};

您将有一个 DOM 元素来容纳新的 DOM:

<div id="result"></div>

并在您的AjaxOptions 您只需指定 OnSuccess 事件,您不应该使用 UpdateTargetId 因为它会立即将新内容注入到 DOM 中,而无需等待图像已加载:

@Ajax.ActionLink("click me", "LoadAction", new AjaxOptions
{
    OnSuccess = "onSuccess",

})

You could use the waitForImages plugin:

var onSuccess = function (result) {
    $('#result').html('loading...');
    $(result).waitForImages(function () {
        $('#result').html(this);
    });
};

Where you would have a DOM element to harbor the new DOM:

<div id="result"></div>

and in your AjaxOptions you would specify the OnSuccess event only, you should not use UpdateTargetId as it will immediately inject the new contents into the DOM without waiting for images to be loaded:

@Ajax.ActionLink("click me", "LoadAction", new AjaxOptions
{
    OnSuccess = "onSuccess",

})
水中月 2025-01-14 15:54:31

我不太确定你在做什么,但我认为这里有一些错误,应该修复这些问题,以更好地帮助我们理解你的意图:

  1. 你的 OnBegin 和 OnSuccess 具有相同的代码。这有效地隐藏了您的 ColumnContainer。
  2. 在 FinishIt 中,您将内容加载到 ColumnContainer(仍处于隐藏状态)中。
  3. load 需要 .load(url, function) 形式,因此可能会失败。
  4. 如果您使用的是不显眼的 ajax,为什么要尝试 .load?

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:

  1. Your OnBegin and OnSuccess have the same code. Which effectively hides your ColumnContainer.
  2. In FinishIt you are loading content into ColumnContainer (which is still hidden).
  3. load expects the form .load(url, function) and probably fails for this reason.
  4. If you are using unobtrusive ajax, why are you trying to .load?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文