【问题标题】:jQuery AJAX using fadeIn + replaceWithjQuery AJAX 使用淡入 + 替换
【发布时间】:2011-09-13 12:15:35
【问题描述】:

关于这个问题有几个问答,但我发现没有一个可以解决我的问题。我对此感到困惑。如果您知道此问题的可用答案,请将我重定向到一个链接。

我的页面有一个空的DIV #posts_insert,其中通过 Ajax 插入新帖子。

    $.ajax({
        url: 'chat/posts_submit/' + <?php echo $page_id; ?>,
        type: 'POST',
        data: $('#posts_form').serialize(),
        dataType: 'html',
        success: function(html) {
            $('#posts_insert').replaceWith(html);
        }
    });

我希望新帖子淡出,替换 #posts_insert。我在success 上使用hide()fadeIn 之前尝试了几次迭代,但我就是无法完成这项工作。

任何想法如何解决这个问题?提前致谢。

【问题讨论】:

  • 你试过这个 $('#posts_insert').replaceWith(html).fadeIn('slow')

标签: jquery hide fadein replacewith


【解决方案1】:

你可以试试:

success: function(html) {
   var $container = $('#posts_insert').parent();
   $container.fadeOut('fast', function() {
      $('#posts_insert').replaceWith(html);
      $container.fadeIn();
   });
}

可能会给你想要的效果。

编辑: 为什么不在 (#posts_insert) 周围放置一个容器,将其淡出,replaceWith(),然后淡入容器?

【讨论】:

  • replaceWith 返回原始元素。所以这将在 #posts_insert 元素中消失,而不是刚刚检索到的 html。
  • 不幸的是不起作用-最终结果与我的原始代码相同-谢谢建议
【解决方案2】:

这样的?

$.ajax({
    url: 'chat/posts_submit/' + <?php echo $page_id; ?>,
    type: 'POST',
    data: $('#posts_form').serialize(),
    dataType: 'html',
    success: function(html) {
        var newContent = $(html);
        $('#posts_insert').fadeOut(function() {
            $(this).replaceWith(newContent.hide());
            newContent.fadeIn();
        });
    }
});

【讨论】:

  • thx @eric - 这个返回一个uncaught exception 错误,不断循环不停
【解决方案3】:

怎么样:

$("#posts_insert").replaceWith(function() {
    return $(html).hide().fadeIn();
});

这是一个工作示例:http://jsfiddle.net/andrewwhitaker/jTLn5/

【讨论】:

  • thx @andrew,你的小提琴非常好——由于某种原因,我再次收到类似@eric 的错误——完整的消息是uncaught exception: [Exception... "Could not convert JavaScript argument arg 0 [nsIDOMViewCSS.getComputedStyle]" nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)" location: "JS frame :: http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js :: &lt;TOP_LEVEL&gt; :: line 16" data: no]
  • @torr: 嗯,也许您尝试与replaceWith() 一起使用的HTML 中有一些奇怪的东西?当您只使用.html() 时,您会收到该错误吗?
  • @andrew - 是的,.html() 也会发生这种情况 - 生成新帖子的 HTML 没有 JS,只有 PHP 和 HTML
  • @torr:我会检查丢失的标签等。可能会运行您通过验证器从服务器返回的 HTML。
  • 该解决方案不会淡出原始内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-02
  • 1970-01-01
  • 2012-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多