$.each(selector) 和 $(selector).each() 有什么区别
之间有什么区别?
$.each($('#myTable input[name="deleteItem[]"]:checked').do_something());
this:和 this:
$('#myTable input[name="deleteItem[]"]:checked').each(function() { do_something });
正在选择并执行操作的表格单元格的 html 看起来像这样:
<td width="20px"><input type="checkbox" class="chkDeleteItem" name="deleteItem[]" value="' . $rowItem['itemID'] . '" /></td>
我已经浏览了 jQuery 文档,但我仍然不明白其中的区别。 (是我的问题还是该文档有时在内容的清晰度上有点“模糊”?)
添加信息:
显然我尝试通用示例使人们感到困惑!以及第一个示例中(以前)缺少的括号。 :(
第一个示例来自我的代码中的一行,该行删除了带有选中的复选框的任何行的 :
$.each($('#classesTable input[name="deleteClasses[]"]:checked').parent().parent().parent().remove());
第二个示例来自这样的情况:我在 #classesTable 中查找任何选中的复选框并删除其 我知道它们做了两种不同的事情
$('#classesTable input[name="deleteClasses[]"]:checked').each(function(){
$('#classesList option[value="' + $(this).attr('value') + '"]').remove();
});
,但还没有达到我能够说“在这种情况下我需要使用 $.each() 和 .each(function() {}”的程度。 )在另一个 仅在某些情况
下它们可以互换吗?
What is the difference between this:
$.each($('#myTable input[name="deleteItem[]"]:checked').do_something());
and this:
$('#myTable input[name="deleteItem[]"]:checked').each(function() { do_something });
The html for the table cell that is being selected and acted upon looks like this:
<td width="20px"><input type="checkbox" class="chkDeleteItem" name="deleteItem[]" value="' . $rowItem['itemID'] . '" /></td>
I've gone over the jQuery documentation, but I still don't understand the difference. (Is it me or is that documentation sometimes slightly "nebulous" in clarity of content?)
Added Info:
Apparently my attempt a generic examples is confusing people! Along with the (previously) missing parenthesis in the first example. :(
The first example comes from a line in my code that removes the <tbody> for any rows with a checkbox that is checked:
$.each($('#classesTable input[name="deleteClasses[]"]:checked').parent().parent().parent().remove());
The second example comes from a situation where I look through the #classesTable for any checked checkboxes and remove its matching item in a dropdown.
$('#classesTable input[name="deleteClasses[]"]:checked').each(function(){
$('#classesList option[value="' + $(this).attr('value') + '"]').remove();
});
I understand that they do two different things, but not to the point that I'd be able to say "I need to use $.each() in this case and .each(function() {}) in another case.
Are they interchangeable at all? Only in some cases? Never?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
描述:
示例
1) 使用
$.each()
函数2) 使用
.each()
方法资源
Description:
Examples
1) Using
$.each()
function2) Using
.each()
methodResources
来自 http://api.jquery.com/jQuery.each:
from http://api.jquery.com/jQuery.each:
您确实希望将
$.each
与不是元素或其他内容的数组一起使用。即:您可以使用
$.each(x...
来遍历它,而不是x.each
:).each
用于元素仅有的 :)You want to really use
$.each
with an array that isn't elements or something. ie:You'd use
$.each(x...
to traverse that instead ofx.each
:).each
is for elements only :)没有功能上的区别。每个 jQuery 对象都拥有一个从
jQuery.fn
继承的.each()
方法。通过调用此对象方法
,jQuery 已经知道要迭代哪个数组(类对象)
。换句话说,它循环遍历当前 jQuery 对象的索引属性
。另一方面,
$.each()
只是一个“辅助工具”,它循环任何类型的Array
或Object
,但是当然您必须告诉该方法您要迭代哪个目标。它还会照顾您是否传入数组或对象,它在底层使用
for-in
或for 循环
执行正确的操作。There is no functional difference. Every jQuery object owns a
.each()
method inherited fromjQuery.fn
. By calling thisobject method
, jQuery already knows whichArray (-like object)
to iterate over. In other words, it loops over theindexed propertys
from the current jQuery object.$.each()
on the other hand is just a "helper tool" which loops over any kind ofArray
orObject
, but of course you have to tell that method which target you want to iterate.It'll also take care of you whether you pass in an Array or object, it does the right thing using a
for-in
orfor loop
under the hood.第一个将对您传入的集合中的元素运行回调函数,但您的代码目前在语法上不正确。
它应该是:
参见:http://api.jquery.com/jQuery.each/
第二个将在您正在运行该函数的集合的每个元素上运行该函数。
请参阅:http://api.jquery.com/each/
The first will run the callback function to the elements in the collection you've passed in, but your code is not syntactically correct at the moment for it.
It should be:
See: http://api.jquery.com/jQuery.each/
The second will run the function on each element of the collection you are running it on.
See: http://api.jquery.com/each/
在第一种情况下,您可以迭代 jQuery 对象以及其他数组项,如下所示:
jQuery.each()
在第二种情况下,您只能遍历 jQuery 对象,如下所示:
.each()
In the first case you can iterate over jQuery objects and also other array items as indicated here:
jQuery.each()
In the second case you can only itterate over jQuery objects as indicated here:
.each()
据我了解
$.each();
循环遍历一个对象或数组,并为您提供每个项目的迭代器和值。$().each();
循环遍历 jQuery 对象列表,并为您提供迭代器和 jQuery 对象。From what I understand
$.each();
loops through an object or array and gives you the iterator and value of each item.$().each();
loops through a list of jQuery objects and gives you the iterator and the jQuery object.取自 http://api.jquery.com/jQuery.each/
Taken from http://api.jquery.com/jQuery.each/