jquery load方法是否提供与Rails replace_html方法等价的ajax功能?

时间:2022-04-16 20:46:30

The jquery load method loads HTML from a remote file and injects it into the DOM. For example, to load the feeds.html file into the div with the ID of feeds, you would do this:

jquery load方法从远程文件加载HTML并将其注入DOM。例如,加载提要。将html文件放入具有提要ID的div中,您可以这样做:

$("#feeds").load("feeds.html");

Is this an alternative to calling a partial with the Rails replace_html method or is the functionality slightly different?

这是用Rails replace_html方法调用部分的替代方法,还是功能略有不同?

page.replace_html 'feeds', :partial => 'main/feeds',
    :locals => {:feed =>  @feed_data }

[EDIT]: As Craig Stuntz points out, replace_html returns Javascript instead of HTML - what's the advantage/disadvantage of this? Does it just mean that the fragment you return has more functional capabilities in the same way that a web page that uses Javascript is more powerful than a regular HTML page? Or is there some other reason for returning Javascript instead of HTML?

正如Craig Stuntz指出的,replace_html返回的是Javascript而不是HTML——这样做的好处和坏处是什么?这是否仅仅意味着您返回的片段具有更多的功能,就像使用Javascript的web页面比普通HTML页面更强大一样?或者返回Javascript而不是HTML还有其他原因吗?

1 个解决方案

#1


6  

The end result is much the same, but they work in a very different way. The load method means that your server returns an HTML fragment, and jQuery inserts it into the page. The replace_html method means that your server is returning JavaScript instead of HTML, and this JavaScript is executed to insert an HTML fragment into the page. So the final resulting HTML is the same, but the traffic between the client and the server is very different.

最终的结果是一样的,但它们的工作方式却大不相同。load方法意味着服务器返回一个HTML片段,jQuery将其插入到页面中。replace_html方法意味着服务器返回的是JavaScript而不是HTML,并执行此JavaScript将HTML片段插入页面。所以最终得到的HTML是相同的,但是客户机和服务器之间的通信是非常不同的。

#1


6  

The end result is much the same, but they work in a very different way. The load method means that your server returns an HTML fragment, and jQuery inserts it into the page. The replace_html method means that your server is returning JavaScript instead of HTML, and this JavaScript is executed to insert an HTML fragment into the page. So the final resulting HTML is the same, but the traffic between the client and the server is very different.

最终的结果是一样的,但它们的工作方式却大不相同。load方法意味着服务器返回一个HTML片段,jQuery将其插入到页面中。replace_html方法意味着服务器返回的是JavaScript而不是HTML,并执行此JavaScript将HTML片段插入页面。所以最终得到的HTML是相同的,但是客户机和服务器之间的通信是非常不同的。