What is the difference between $.each(selector) and $(selector).each()

前端 未结 8 868
夕颜
夕颜 2020-12-07 12:59

What is the difference between this:

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

and this:



        
8条回答
  •  失恋的感觉
    2020-12-07 13:48

    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

    • https://api.jquery.com/jquery.each/
    • http://api.jquery.com/each/
    • jQuery to loop through elements with the same class

提交回复
热议问题