$ .each(selector)和$(selector).each()有什么区别 [英] What is the difference between $.each(selector) and $(selector).each()
问题描述
这有什么区别:
$.each($('#myTable input[name="deleteItem[]"]:checked').do_something());
和这个:
$('#myTable input[name="deleteItem[]"]:checked').each(function() { do_something });
正在被选择并作用的表格单元格的html看起来像这样:
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>
我已经阅读了jQuery文档,但是我仍然不了解它们之间的区别. (是我还是该文档有时在内容的清晰度上有些模糊"?)
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?)
添加的信息:
显然我的尝试是一个通用示例,令人困惑!以及第一个示例中(以前)缺少的括号. :(
Apparently my attempt a generic examples is confusing people! Along with the (previously) missing parenthesis in the first example. :(
第一个示例来自我代码中的一行,该行删除了< tbody>对于带有已选中复选框的任何行:
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());
第二个示例来自以下情况:我在#classesTable中查看所有选中的复选框,然后在下拉列表中删除其匹配项.
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();
});
我知道他们做了两种不同的事情,但是我不能说在这种情况下,我需要使用$ .each(),而在另一种情况下,需要使用.each(function(){}).情况.
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?
推荐答案
说明:
.each
是用于仅对jQuery进行迭代的迭代器jQuery.each
($.each
)是常规对象时进行对象收集 用于遍历JavaScript对象和数组的函数.
.each
is an iterator that is used to iterate over only jQuery objects collection whilejQuery.each
($.each
) is a general function for iterating over JavaScript objects and arrays.
示例
1)使用$.each()
函数
Examples
1) Using $.each()
function
var myArray = [10,20,30];
$.each( myArray, function(index, value) {
console.log('element at index ' + index + ' is ' + value);
});
//Output
element at index 0 is 10
element at index 1 is 20
element at index 2 is 30
2)使用.each()
方法
2) Using .each()
method
$('#dv').children().each(function(index, element) {
console.log('element at index ' + index + 'is ' + (this.tagName));
console.log('current element as dom object:' + element);
console.log('current element as jQuery object:' + $(this));
});
//Output
element at index 0 is input
element at index 1 is p
element at index 2 is span
资源
- https://api.jquery.com/jquery.each/
- http://api.jquery.com/each/
- 用jQuery遍历具有相同类的元素
- https://api.jquery.com/jquery.each/
- http://api.jquery.com/each/
- jQuery to loop through elements with the same class
Resources
这篇关于$ .each(selector)和$(selector).each()有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!