$ .each (selector)와 $ (selector) .each ()의 차이점은 무엇입니까?
이것의 차이점은 무엇입니까?
$.each($('#myTable input[name="deleteItem[]"]:checked').do_something());
이:
$('#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 문서를 살펴 보았지만 여전히 차이점을 이해하지 못합니다. (나인가 아니면 그 문서가 내용의 명확성에있어 때때로 약간 "모호한"것인가?)
추가 정보 :
분명히 내 시도는 일반적인 예입니다. 첫 번째 예에서 (이전) 누락 된 괄호와 함께. :(
첫 번째 예제는 체크 박스가있는 모든 행에 대해 <tbody>를 제거하는 코드 줄에서 나옵니다.
$.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 () {})를 사용해야합니다.
그것들은 전혀 바꿔 사용할 수 있습니까? 어떤 경우에만? 못?
기술:
.each
는 jQuery 객체 컬렉션 만 반복하는 데 사용되는 반복기이며jQuery.each
($.each
)는 javascript 객체 및 배열을 반복하는 일반 함수입니다.
예 :
$ .each ()를 사용하는 자바 스크립트 배열 (또는 js 객체) :
var myArray = [10,20,30];
jQuery.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
.each ()를 사용하는 jQuery 객체
$('#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
더 많은 예제 + 세부 정보를 찾고 있다면 $ .each 대 .each ()
자원
jQuery.each
($.each
) 문서 : https://api.jquery.com/jquery.each/.each
문서 : http://api.jquery.com/each/- 동일한 클래스의 요소를 반복하는 jQuery
에서 http://api.jquery.com/jQuery.each :
$ .each () 함수는 jQuery 객체를 독점적으로 반복하는 데 사용되는 .each ()와 동일하지 않습니다. $ .each () 함수는 맵 (JavaScript 객체)이든 배열이든 모든 컬렉션을 반복하는 데 사용할 수 있습니다.
$.each
요소 등이 아닌 배열과 함께 실제로 사용하고 싶습니다 . 즉 :
var x = ["test", "test2"];
당신 $.each(x...
은 대신 그것을 횡단하는 데 사용 합니다 x.each
:)
.each
요소 전용입니다. :)
기능적 차이는 없습니다. 모든 jQuery를 객체는 소유 .each()
에서 상속 방법을 jQuery.fn
. this를 호출함으로써 object method
jQuery는 Array (-like object)
반복 할 대상 을 이미 알고 있습니다. 즉, indexed propertys
현재 jQuery 객체에서 반복 됩니다.
$.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.
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/
In the first case you can iterate over jQuery objects and also other array items as indicated here:
In the second case you can only itterate over jQuery objects as indicated here:
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.
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.
'Nice programing' 카테고리의 다른 글
다른 저장소에서 Git 가져 오기 (0) | 2020.10.12 |
---|---|
커밋 순서 변경 (0) | 2020.10.12 |
vim이 컴파일 된 옵션을 어떻게 확인할 수 있습니까? (0) | 2020.10.12 |
IntelliJ IDEA 13에서 여러 줄로 된 할 일을 사용할 수 있습니까? (0) | 2020.10.12 |
Android 버튼에 setOnTouchListener가 호출되었지만 performClick을 재정의하지 않습니다. (0) | 2020.10.12 |