$(slector).load(' page.html#id')与$ .get并过滤/查找#id [英] $(slector).load('page.html #id') vs $.get and filter/find #id
问题描述
我想知道在性能上是否有差异(或根本没有差异)
I would like to know if there is any difference in perfomance (or difference at all)
在此之间:
$(selector).load('page #myid');
和
$.get('page',function(data){
$(selector).html( $(data).find('#myid') );
/* depending the dom tree it should be .filter() */
})
推荐答案
我会说使用 load() 或 .get() 更多与您的意图和要求有关,而不是与绩效有关.
I would say using load() or .get() is more to do with your intentions and requirements rather than performance.
Load()
.load()
用于轻松/简单地加载文档或文档的一部分.
它将加载完整的文档,然后使用 innerHTML
解析HTML以仅返回您感兴趣的部分.
.load()
is for easy/simple loading of a document or a portion of it.
It will load the complete document and then using innerHTML
parse the HTML to return only the portion you are interested in.
如果只需要一种将某些内容加载到div中的快速方法,则可以使用 $(element).load(url,选择器)
.我认为 load()
为此进行了优化.
If you only need a fast way of loading some content into a div then $(element).load(url, selector)
is the way to go. I would assume load()
is optimised for just that.
获取()
与 load()
不同, .get()
允许设置 dataType
以获得更大的灵活性,例如跨域请求的JSONP.
Unlike load()
, .get()
allows setting of the dataType
for greater flexibility such as JSONP for cross-domain requests.
它还实现了 promise
接口,为它提供了 Deferred Object
的所有功能.
It also implements the promise
interface, giving it all the features of a Deferred Object
.
从文档中
Promise接口还允许jQuery的Ajax方法,包括$ .get(),以链接多个.done()、. fail()和.always()回调在单个请求上,甚至可以在请求可能已完成.如果请求已经完成,则回调立即被触发.
The Promise interface also allows jQuery's Ajax methods, including $.get(), to chain multiple .done(), .fail(), and .always() callbacks on a single request, and even to assign these callbacks after the request may have completed. If the request is already complete, the callback is fired immediately.
摘要
在非常基本的级别上,简单的 .load(url,选择器)
和 .get(url,回调)
之间应该没有值得的性能差异..使用任何一个都可以.
On a very basic level, there should be no worth-while performance difference between a simple .load(url, selector)
and .get(url, callback)
. Using either should be fine.
我想,这里没有对与错.使用适合您需求的东西.
I would think, there is no right or wrong here as such. Use what ever suits your needs.
问自己类似的问题
您是否需要跨域(JSONP)?
您是否需要链接 .fail()
, .done()
等??
您是否需要 get()
提供的 load()
不具备的功能?
...
Do you need cross-domain (JSONP)?
Do you need to chain .fail()
, .done()
, etc. ?
Do you need any of the features get()
offers which load()
does not have?
...
如果您使用 load()
或 get()
,则取决于您的要求.在这种情况下,性能是次要的.
If you use load()
or get()
depends on your requirements. Performance is secondary in this case.
这篇关于$(slector).load(' page.html#id')与$ .get并过滤/查找#id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!