📌  相关文章
📜  如何在 JavaScript 代码示例中选择元素

📅  最后修改于: 2022-03-11 15:03:57.533000             🧑  作者: Mango

代码示例1
// Selecting an Element by id:
document.getElementById("elem-id");
// Selecting  Elements by class:
document.getElementsByClassName("elem-class");
// Selecting Elements by tag name: 
document.getElementsByTagName("div");
//  Selecting Elements by name:
document.getElementsByName("elem-name");
/* This method is like a jQuery selector 
( #elemId -> id, .elemClass -> class, h1 -> tag name) */
document.querySelector(".container"); // class
document.querySelector("#demo"); // id
document.querySelector("div"); // tag name