我不知道如何获取标题后的任何特定段落。
以下是我的 HTML
文件脚本:
<!doctype html>
<html>
<head><title>SAM.com</title>
</head>
<body><h1>This is Heading</h1>
<p>This is paragraph 1</p>
<p>This is paragraph 2</p>
<p>This is paragraph 3</p>
</body>
</html>
注意:我想在第一级标题之后显示第 3 段。
最佳答案
如果您正在寻找 CSS 解决方案,您可以使用带有 order
的 Flexbox 布局属性设置:
body {
display: flex;
flex-direction: column;
}
p {
order: 1;
}
p:nth-child(4) {
order: 0;
}
<h1>This is Heading</h1>
<p>This is paragraph 1</p>
<p>This is paragraph 2</p>
<p>This is paragraph 3</p>
如果您正在寻找 JavaScript 解决方案,可以使用一些基本的 DOM 操作方法,例如 insertBefore()
也能达到目的:
document.body.insertBefore(
document.getElementsByTagName('p')[2],
document.getElementsByTagName('h1')[0].nextSibling
);
<h1>This is Heading</h1>
<p>This is paragraph 1</p>
<p>This is paragraph 2</p>
<p>This is paragraph 3</p>
关于html - 从任何地方获取标题下方的段落,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52945243/