What's the equivalent of 'getElementsByTagName' in jQuery?

后端 未结 4 1120
说谎
说谎 2020-12-29 03:24

What\'s the equivalent of getElementsByTagName() in jQuery? I just want to create a collection of elements in jQuery so I can iterate through them and do someth

4条回答
  •  感动是毒
    2020-12-29 03:54

    $("tagnamehere")
    

    So:

    $("div").each(function() {
        // do something exciting with each div
        $(this).css("border", "1px solid red");
    
        // do something by directly manipulating the wrapped DOM element
        this.style.border = "1px solid red";
    
        // do something only if this particular div has a class of 'pretty'
        if($(this).hasClass("pretty")) {
            $(this).text("I am the pretty one");
        }
    });
    

    or just:

    // apply some css to all div elements
    $("div").css("border", "1px solid red");
    

    Keep in mind that when you use jQuery to select a number of elements, e.g. $("span"), any method you invoke on the object will happen on all matched elements. Think of it as 'implicit iteration' - e.g. $("span").hide(); will hide all span elements on the page.

    See:

    • http://www.w3.org/TR/css3-selectors/
    • http://api.jquery.com/category/selectors/
    • http://api.jquery.com/each/

提交回复
热议问题