且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么相当于jQuery中的'getElementsByTagName'?

更新时间:2022-06-23 05:32:38

$("tagnamehere")

所以:

$("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");
    }
});

或只是:

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

请记住,当您使用jQuery选择多个元素时,例如: $(span),您在对象上调用的任何方法都将发生在所有匹配的元素上。将其视为隐式迭代 - 例如 $(span)。hide(); 将隐藏页面上的所有span元素。

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.

请参阅:

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