html - HTML选择没有类的特定元素
问题描述
正如标题所说,我不知道如何只选择第一个h1“我要选择这个”
特别是不使用类,只使用 HTML 文本中的第一个 h1
解决方案
非常简单的querySelector就可以了
// 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>
推荐阅读
- asp.net - 如何在 ASP.Net Core 中实现自定义控制器动作选择?
- python - Tensorflow Quantization - Failed to parse the model: pybind11::init(): factory function returned nullptr
- mongodb - Mongoose aggregate doesn't return unmatched subdocuments
- python - I have run this many times but it showing that path is inncorrect
- python - 作为函数/循环参数的 DataFrame 列表
- swift - 使用 UIScrollView 在 UIView 上绘制错误的观点
- postgresql - 使用 glusterfs 作为存储的 k8s 上的 postgres
- java - 如何以编程方式通过android中的图像链接设置壁纸?
- scala - 带有空间的Scala Dataframe列保存为databricks表
- oop - 编写一个包含它是实例化的类的单例是常规的吗?