$.each(selector) 和 $(selector).each() 有什么区别

发布于 2024-11-19 09:57:35 字数 1124 浏览 0 评论 0原文

之间有什么区别?

$.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

墨小墨 2024-11-26 09:57:35

描述:

.each 是一个迭代器,仅用于迭代 jQuery
对象集合,而 jQuery.each ($.each) 是通用的
用于迭代 JavaScript 对象和数组的函数。


示例

1) 使用 $.each() 函数

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() 方法

$('#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

资源

Description:

.each is an iterator that is used to iterate over only jQuery
objects collection while jQuery.each ($.each) is a general
function for iterating over JavaScript objects and arrays.


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

Resources

阳光下慵懒的猫 2024-11-26 09:57:35

来自 http://api.jquery.com/jQuery.each

$.each() 函数不一样
如.each(),用于迭代,
专门针对 jQuery 对象。这
$.each() 函数可用于
迭代任何集合,无论是
它是一个地图(JavaScript 对象)或
数组。

from http://api.jquery.com/jQuery.each:

The $.each() function is not the same
as .each(), which is used to iterate,
exclusively, over a jQuery object. The
$.each() function can be used to
iterate over any collection, whether
it is a map (JavaScript object) or an
array.

嘦怹 2024-11-26 09:57:35

您确实希望将 $.each 与不是元素或其他内容的数组一起使用。即:

var x = ["test", "test2"];

您可以使用 $.each(x... 来遍历它,而不是 x.each :)

.each 用于元素仅有的 :)

You want to really use $.each with an array that isn't elements or something. ie:

var x = ["test", "test2"];

You'd use $.each(x... to traverse that instead of x.each :)

.each is for elements only :)

挽清梦 2024-11-26 09:57:35

没有功能上的区别。每个 jQuery 对象都拥有一个从 jQuery.fn 继承的 .each() 方法。通过调用此对象方法,jQuery 已经知道要迭代哪个数组(类对象)。换句话说,它循环遍历当前 jQuery 对象的索引属性

另一方面,$.each() 只是一个“辅助工具”,它循环任何类型的 ArrayObject,但是当然您必须告诉该方法您要迭代哪个目标。
它还会照顾您是否传入数组或对象,它在底层使用 for-infor 循环 执行正确的操作。

There is no functional difference. Every jQuery object owns a .each() method inherited from jQuery.fn. By calling this object method, jQuery already knows which Array (-like object) to iterate over. In other words, it loops over the indexed propertys from the current jQuery object.

$.each() on the other hand is just a "helper tool" which loops over any kind of Array or Object, 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 or for loop under the hood.

橪书 2024-11-26 09:57:35

第一个将对您传入的集合中的元素运行回调函数,但您的代码目前在语法上不正确。

它应该是:

$.each($('#myTable input[name="deleteItem[]"]:checked'), do_something);

参见: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:

$.each($('#myTable input[name="deleteItem[]"]:checked'), do_something);

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/

已下线请稍等 2024-11-26 09:57:35

在第一种情况下,您可以迭代 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()

世界如花海般美丽 2024-11-26 09:57:35

据我了解 $.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.

花期渐远 2024-11-26 09:57:35

取自 http://api.jquery.com/jQuery.each/

$.each() 函数与 .each() 不同,后者用于专门迭代 jQuery 对象。 $.each() 函数可用于迭代任何集合,无论它是映射(JavaScript 对象)还是数组。对于数组,回调每次都会传递一个数组索引和相应的数组值。 (该值也可以通过 this 关键字访问,但 Javascript 总是将 this 值包装为一个对象,即使它是一个简单的字符串或数字值。)该方法返回其第一个参数,即被迭代的对象。

Taken from http://api.jquery.com/jQuery.each/

The $.each() function is not the same as .each(), which is used to iterate, exclusively, over a jQuery object. The $.each() function can be used to iterate over any collection, whether it is a map (JavaScript object) or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time. (The value can also be accessed through the this keyword, but Javascript will always wrap the this value as an Object even if it is a simple string or number value.) The method returns its first argument, the object that was iterated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文