是否存在导致单击的链接更改目标站点上的属性的 CSS 规则?

初学者问题。假设我有 4 个不同的链接指向同一网页,是否可以根据将我带到那里的链接来更改目标页面的属性?示例:我单击 link1,目标位置的背景为黄色。link2 导致它变黑。

所以我的问题是我是否需要为 4 个不同的链接创建 4 个不同的网站副本?或者是否有 CSS 规则或其他规则可以实现相同的效果,而无需创建 4 个副本?


翻翻过去那场雪
浏览 49回答 1
1回答

炎炎设计

可以使用css 中的:target伪选择器来根据URL 的#anchor更改样式。例如下面的例子,www.mysite.com/test.html#blackdiv#black:target {&nbsp;background-color: black;}div#yellow:target {&nbsp;background-color: yellow;}div.page {&nbsp;min-height: 100vh;}<!DOCTYPE html><html><head>&nbsp; <meta charset="UTF-8">&nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; <title>Test</title>&nbsp; <link rel="stylesheet" href="style.css"></head>&nbsp; <body>&nbsp; &nbsp; <div id="black">&nbsp; &nbsp; &nbsp; <div id="yellow">&nbsp; &nbsp; &nbsp; &nbsp; <div class="page">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h1>Content</h1>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; </body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript