Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用不引人注目的JS更新页面的两个不同部分(在Rails w/jQuery中)_Jquery_Ruby On Rails_Ajax_Unobtrusive Javascript - Fatal编程技术网

使用不引人注目的JS更新页面的两个不同部分(在Rails w/jQuery中)

使用不引人注目的JS更新页面的两个不同部分(在Rails w/jQuery中),jquery,ruby-on-rails,ajax,unobtrusive-javascript,Jquery,Ruby On Rails,Ajax,Unobtrusive Javascript,当用户单击我博客上的排序链接时,我想使用AJAX来获取新文章和更新排序链接部分。使用RJS会很容易,但我想让我的JS保持低调,只使用数据而不使用代码响应AJAX请求 以下是我现在正在做的: $('a.order_by').livequery('click', function() { $.get(this.href, null, function(data) { // The server returns a blob of HTML with a div#posts

当用户单击我博客上的排序链接时,我想使用AJAX来获取新文章和更新排序链接部分。使用RJS会很容易,但我想让我的JS保持低调,只使用数据而不使用代码响应AJAX请求

以下是我现在正在做的:

  $('a.order_by').livequery('click', function() {
    $.get(this.href, null, function(data) {
      // The server returns a blob of HTML with a div#posts
      // and a div#sorting
      $("#posts").partial_html(data, "#posts");
      $("#sorting").partial_html(data, "#sorting");
    });
    return false;
  });
下面是部分html函数:

// Stolen from the jQuery source for $.load()
jQuery.fn.partial_html = function(data, selector) {
    $(this).html($("<div/>").append(data.replace(/<script(.|\s)*?\/script>/g, "")).find(selector));
}

很明显这有点恶心-什么是正确的方法?使用两个$.load将非常容易,但这需要一个多余的HTTP请求。

我将使用$.load将返回的数据存储在变量think caching中,然后使用jQuery从该缓存中选择div,并使用$.html将其附加到DOM中


对我有用。虽然我不确定这样做是否正确。

我想这可能与我刚刚发布的问题有关。让我们看看是否能找到正确的解决方案

  $("#posts").html($(data).filter("#posts").html());
  $("#sorting").html($(data).filter("#sorting").html());