html - 在 HTML/CSS 中单击时如何删除目标中的样式

标签 html css target

我有一个示例代码,当我单击指定的超链接时,它会尝试为目标着色。

我正在制作一些元素,并且在这个元素上具有相同的功能。当我单击目标时,我希望为已链接的元素着色,并且在代码中它正在工作,但是当我再次单击屏幕中的其他任何位置时,我想删除目标的颜色。

p:target {
  background-color: gold;
}

/* Add a pseudo-element inside the target element */
p:target::before {
  font: 70% sans-serif;
  content: "►";
  color: limegreen;
  margin-right: 0.25em;
}

/* Style italic elements within the target element */
p:target i {
  color: red;
}
<h3>Table of Contents</h3>
<ol>
  <li><a href="#p1">Jump to the first paragraph!</a></li>
  <li><a href="#p2">Jump to the second paragraph!</a></li>
  <li>
    <a href="#nowhere">
      This link goes nowhere, because the target doesn't exist.
    </a>
  </li>
</ol>
<h3>My Fun Article</h3>
<p id="p1">
  You can target <i>this paragraph</i> using a URL fragment. Click on the link above to try out!
</p>
<p id="p2">
  This is <i>another paragraph</i>, also accessible from the links above. Isn't that delightful?
</p>

我想在单击屏幕中的任意位置时删除目标的颜色。我该怎么做?

最佳答案

您可能需要一点 JavaScript:

// Get the target element that you want to remove the color from
const target = document.querySelector('#my-target');

// Add a click event listener to the entire document
document.addEventListener('click', () => {
    // When the document is clicked, remove the color from the target element
    target.style.color = '';
});

关于html - 在 HTML/CSS 中单击时如何删除目标中的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74635120/

相关文章:

javascript - 计算几何Javascript

javascript - 尝试在用户单击时为 a div 设置动画。

android - 如何更改构建目标(2.2 到 2.3.3)?或尝试后恢复

javascript - 如何通过单击 Highchart 图表中的列/栏来打开新窗口/选项卡?

ios - 构建错误 : What's "-licucore" and how can I fix this error?

html - 更改 HTML 正常流程

javascript - Jquery 滚动脚本 - 菜单位于部分顶部而不是内部

html - Bootstrap 列布局排序?

css - 水平滚动中断 overflow-x :hidden in safari and firefox

javascript - 如何编辑此函数以仅在输入字段完成时运行?