jquery 选择器
<input type="text" name="name" value="" id="name"/>
<input type="text" name="subject" value="" id="subject" />
<input type="text" name="message" value="" id="message" />
有人可以告诉我,如果我单独选择输入或将它们放在一个交换中,有什么区别吗?
就像
var name = jQuery('#name');
var subject= jQuery('#subject');
var message = jQuery('#message');
与将它们放在一次交换中相比,
jQuery(":input").each(function(){....};)
我的意思是,每次我调用
如果从我的示例中我这样做,
<input type="text" name="message" value="" id="message" />
<input type="text" name="name" value="" id="name"/>
这意味着我查询了最后一个输入表单,然后查询了第一个输入表单,这不会使流程变慢吗?
<input type="text" name="name" value="" id="name"/>
<input type="text" name="subject" value="" id="subject" />
<input type="text" name="message" value="" id="message" />
Can someone tell me the difference if I selected individually the inputs or getting them in one swap.
like
var name = jQuery('#name');
var subject= jQuery('#subject');
var message = jQuery('#message');
as compared to getting them in one swap
jQuery(":input").each(function(){....};)
i mean wouldn't jquery have to go back to the document root everytime I call <input type="text" name="name" value="" id="name"/>
?
and if from my example I do this
<input type="text" name="message" value="" id="message" />
<input type="text" name="name" value="" id="name"/>
which means i queried the last input form then queried back the first input form, wouldnt it make the flow slower?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
jQuery('#name');
会比jQuery(":input")
快很多。jQuery('#name')
会将选择器作为 ID 进行匹配,并通过 ID 返回项目,而:input
将遍历整个 dom 来查找 input 类型的元素。如果您确实担心选择器性能,请查看 http://www.paulirish.com/perf
jQuery('#name');
will be faster thenjQuery(":input")
, by a lot.jQuery('#name')
will match the selector as an ID and return the item by ID, while:input
will traverse the entire dom for elements that are of type input.If you're really worried about selector performance, take a look at http://www.paulirish.com/perf
正如 Peeter 所说,使用 ID 选择器会快得多。
但是,您可以结合两者的优点 - 例如使用
$('#id-of-your-form :input')
。As Peeter said, using an ID selector will be much faster.
However, you can combine the advantages of both - e.g. use
$('#id-of-your-form :input')
.正如 @ThiefMaster 上面提到的,您可以使用
$('#id-of-your-form :input')
进行组合,但它也存在性能问题,因为 jQuery 的 Sizzle 选择器引擎从右侧解析选择器向左。为了获得更多性能,您可以使用以下方式
这将导致在具体 DOM 元素的上下文中进行选择器搜索。
另请注意,它与以下内容不一样:
这将在内部“重构”,例如
As metioned above by @ThiefMaster you can combine by using
$('#id-of-your-form :input')
but it is also has perfomace issue because jQuery's Sizzle selector engine is parsing selector from right to left.To get more perfomace you can use the following way
This will lead to selector search in context of concrete DOM element.
Also note, what it is not the same as
This will be internally "refactored" like
如果您使用 id 选择器(例如
#name
),那么它可以使用document.getElementById
方法,这是非常高效的。使用像:input
这样的选择器至少需要多次调用getElementsByTagName
,并循环遍历所有找到的元素以检查它们到底是什么。有些选择器(例如类选择器)甚至更昂贵,因为它需要循环遍历页面中的所有元素以查找匹配的元素。如果您通过 id 选择元素,那么您执行的顺序并不重要。 jQuery 单独评估每个选择器,它不希望您以任何特定顺序查询元素。
您可以使用像
#name,#subject,#message
这样的选择器来获取同一个 jQuery 集合中的所有三个元素,如果您想对它们使用相同的方法,这会更有效。If you use an id selector (like
#name
), then it can use thedocument.getElementById
method, which is very efficient. Using a selector like:input
will at least require several calls togetElementsByTagName
, and looping through all the found elements to check what they really are. Some selectors, like a class selector, are even more expensive as it requires to loop through all elements in the page to look for the elements that match.If you select elements by id, then it doesn't matter which order you do that. jQuery evaluates each selector separately, it doesn't expect you to query the elements in any specific order.
You can use a selector like
#name,#subject,#message
to get all three elements in the same jQuery collection, which is more efficient if you want to use the same method on them.如果你真的想比 jQuery 更快地进行查询,如下所示:
jQuery:
First select the element that is
家长..
从父级中选择输入元素..
(只会从容器中查询,而不是整个 DOM 对象)
If you really want to speed up your query than jQuery like this:
jQuery:
First select the element that is
parent..
Select input element FROM parent..
(will only do query from container not the whole the DOM-object)