提问者:小点点

HTML选择不带类的特定元素


就像标题说的,不知道怎么只选第一个h1“我要选这个”

特别是不使用类,只使用HTML文本中的第一个h1


共3个答案

匿名用户

非常简单的querySelector就可以了

null

// first h1 in header

console.log(
  document.querySelector("header > h1").textContent
)

// first h1 in document:

console.log(
  document.querySelector("h1").textContent
)
header > h1 {
  color: green
}
<header>
  <h1>I want to select this</h1>
  <div>
    <h1>Welcome to the world!</h1>
    <p>I don't like apples</p>
  </div>
</header>

匿名用户

使用此

h1:first-of-type{

}

祝你好运。

匿名用户

您可以使用此选项,选择父级,然后选择h1类型的第一个子级

null

   header > h1:first-child{
       color:red;
   }

相关问题