Jquery gets the elements of a class and prints out jQuery.fn.init (0), not the HTMLCollection () collection.

use jquery to get the element of a class, the console prints out jQuery.fn.init (0), and does not get the element of this kind of selector, how can I get it? Use jquery to get the HTMLCollection () collection?

Native JavaScript prints out HTMLCollection ().

jquery prints out jQuery.fn.init (0).

attach my code

var e = document.getElementsByClassName("star");
console.log(e);


$(document).ready(function(){
    console.log($(".star"));
    $(".star").click(function () {
        $(this).css = ("color", "-sharp00F7DE");
        console.log($(this).tagName);
    });
});
Mar.07,2021

  1. need to know the JQuery source code;

    jQuery = function( selector, context ) {
        return new jQuery.fn.init( selector, context, rootjQuery );
    }
  2. The find function is called in
  3. int; in the find function, you will find that the contains function is called;
  4. in the end, JQuery uses the Sizzle selector engine; part of the code in sizzle is as follows:

    // Speed-up: Sizzle(.CLASS)
    /*
     * class:.class
     * :
     * m = match[3]:class
     * support.getElementsByClassName pgetElementsByClassName
     * context.getElementsByClassName getElementsByClassName
     * */            
        else if ((m = match[3]) && support.getElementsByClassName
                && context.getElementsByClassName) {
            push.apply(results, context.getElementsByClassName(m));
            return results;

jquery wraps the obtained element and becomes a jquery object, so there is a corresponding jquery object method. If you want to use the native js method for jquery , you need to convert it to the native HTMLelement object, with the following methods:

$('body')//jq 
$('body').toArray()
$('body')[0]
Array.from($('body'))
Menu