$(slector).load('page.html即可#ID')VS $不用彷徨和过滤器/找到#ID [英] $(slector).load('page.html #id') vs $.get and filter/find #id

查看:190
本文介绍了$(slector).load('page.html即可#ID')VS $不用彷徨和过滤器/找到#ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何区别的在性能比较(或差的话)

之间这样的:

  $(选择).load(页面#myid');
 

  $。获得(页面,功能(数据){
     $(选择)。html的($(数据).find('#身份识别码'));
     / *根据DOM树应该是.filter()* /
})
 

解决方案

我会说使用 负载( ) 获得() 是更多的是与你的意图和要求,而不是性能。

负荷()


.load()是一个文档或它的一部分的轻松/简单的加载。
这将加载完整的文档,然后使用的innerHTML 解析HTML返回只有你感兴趣的部分。

如果你只需要加载一些内容放到一个div然后 $(元素).load(URL,选择器)的快捷方式是要走的路。我会假设负载()针对这一点进行了优化。

获取()


负载()获得()允许数据类型设置有更大的灵活性,如JSONP跨域请求。

它还实现了接口,给它一个延迟的对象的所有特性

从文档:

  

无极界面还允许的jQuery的Ajax方法,包括   $。获得(),把多个.done(),.fail(),和。总是()回调   在单个请求,甚至分配后的这些回调   请求可能已经完成。如果请求已经完成时,   回调立即被解雇。

摘要


在一个非常基本的水平,不应该有任何的价值,而一个简单的 .load(URL,选择器)之间的性能差异不用彷徨(URL,回调)。无论是使用应罚款。

我会想,没有对错本身。使用什么都适合您的需要。

问问你自己问类似:

您需要跨域(JSONP)?
您是否需要链 .fail() .done()等?
你需要的任何功能的get()提供哪些负载()没有?
......

如果您使用负载()的get()取决于您的要求。性能是次要在这种情况

I would like to know if there is any difference in perfomance (or difference at all)

Between this:

$(selector).load('page #myid');

and

$.get('page',function(data){
     $(selector).html( $(data).find('#myid') );
     /* depending the dom tree it should be .filter() */
})

解决方案

I would say using load() or .get() is more to do with your intentions and requirements rather than performance.

Load()


.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.

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.

Get()


Unlike load(), .get() allows setting of the dataType for greater flexibility such as JSONP for cross-domain requests.

It also implements the promise interface, giving it all the features of a Deferred Object.

From the docs:

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.

Summary


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.

Ask yourself question similar to:

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?
...

If you use load() or get() depends on your requirements. Performance is secondary in this case.

这篇关于$(slector).load('page.html即可#ID')VS $不用彷徨和过滤器/找到#ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆