简体   繁体   English

如果访问了锚标记的父元素,则设置其样式

[英]Style the parent element of an anchor tag if it's visited

I've been using HTML for a long time and never really had to do/tried something like this. 我已经使用HTML很长一段时间了,从来没有真正做过这样的事情。 I have an anchor tag inside a paragraph tag , like so: 我在段落标记中有一个 标记 ,如下所示:

<p>Hello <a href="path/page.html">World</a></p>

I was wondering if there is a way to change the style the p element if a has already been visited, using nothing but CSS. 我在想,如果有改变风格的方式p元素,如果a已经被访问过,只是用普通的CSS。 I know JavaScript can do this and by any means feel free to post a Vanilla JS answer if you will for reference, but my question really is about how to do it using just CSS. 我知道JavaScript可以做到这一点,如果你愿意参考,可以随意发布一个Vanilla JS答案,但我的问题实际上是关于如何使用CSS来做到这一点。

Since I predict someone will say something like "Is the parent tag really necessary?" 因为我预测有人会说“父母标签真的有必要吗?” , I know HTML5 and CSS3 brought whole new ways to style and manipulate content without the need for extra tags, but that's not the point of the question. ,我知道HTML5CSS3为样式和操作内容带来了全新的方式,而不需要额外的标签,但这不是问题的关键。 The point is of how to do it if one ever needs to (and in my case, I do) . 重点是如果有人需要(在我的情况下,我这样做)如何做到这一点

There are a couple of issues at play here. 这里有几个问题在起作用。

  1. You can't do this with pure CSS because currently their is no parent selector. 您无法使用纯CSS执行此操作,因为目前它们不是父选择器。 As Hashem noted above, it is currently included in the working draft for CSS4 . 正如Hashem上面提到的, 它目前已包含在CSS4的工作草案中
  2. Questions such as this suggest that it might be possible using JavaScript, to check the colour of the link for example, and deduce from that, that the link is visited. 诸如此类的问题表明,可以使用JavaScript来检查链接的颜色,例如,并从中推断出链接被访问。 However, I've just tried on Firefox 27, and it no longer works for me. 但是,我刚试过Firefox 27,它不再适用于我。 I'd speculate that it's seen as a security issue, seeing as you could potentially exploit it to work out a user's particular internet history. 我推测它被视为一个安全问题,因为您可能会利用它来计算用户的特定互联网历史记录。

Initially I suggested looking at using the ::before selector to try to achieve something, but after a little digging I came across this article (and this answer from SO) which might make for an interested read. 最初,我建议使用::before选择器来尝试实现某些功能,但经过一番挖掘后,我遇到了这篇文章 (以及SO的答案) ,这可能会引起感兴趣的阅读。

See this JSFiddle ( updated to native JS ) for my workings. 请参阅此JSFiddle更新 为本机JS )以了解我的工作原理。 It's worth noting I did try the native JavaScript check as mentioned in the above answer, as well as using the :visited selector in jQuery, but neither worked. 值得注意的是,我确实尝试了上面回答中提到的本机JavaScript检查,以及在jQuery中使用:visited选择器,但都没有工作。

JS JS

var anchors = document.getElementsByTagName("a");
for(var i = 0; i < anchors.length; i ++) {
    var anchor = anchors[i];
    console.log(document.defaultView.getComputedStyle(anchor, null).getPropertyValue('color'));       
}

HTML HTML

<p>
    <a href="http://google.com">Google</a>
</p>
<p>
    <a href="http://nowayjose.com">Never visited</a>
</p>

CSS CSS

a { color: #00FF00; }
a:visited { color: #FF0000; }

The potential security implications of how the ! 如何潜在的安全隐患! selector in CSS4 would play out with :visited rules are pretty interesting, given that you could style a different element and then potentially query that element for a style. CSS4中的选择器会发挥作用:visited规则非常有趣,因为您可以设置不同的元素,然后可能会查询该元素的样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM