如何写一个:悬停在内联CSS? [英] How to write a:hover in inline CSS?

查看:175
本文介绍了如何写一个:悬停在内联CSS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个案例,我必须写入内嵌CSS代码,我想在锚点上应用悬停样式。

I have a case where I must write inline CSS code, and I want to apply a hover style on an anchor.

如何使用<$ c $

How can I use a:hover in inline CSS inside the HTML style attribute?

推荐答案

简短的答案:你可以在HTML样式属性中的内嵌CSS中调用 a:hover 不能。

Short answer: you can't.

长答案:你不应该。

提供类名称或ID,并使用样式表应用样式。

Give it a class name or an id and use stylesheets to apply the style.

:hover 是伪选择器,适用于 CSS ,只有在样式表中有意义。没有任何内联样式等价物(因为它没有定义选择条件)。

:hover is a pseudo-selector and, for CSS, only has meaning within the style sheet. There isn't any inline-style equivalent (as it isn't defining the selection criteria).

回应OP的意见:

请参阅 Totally Pwn CSS with Javascript 了解如何动态添加CSS规则的脚本。另请参阅 更改样式表 的一些理论

See Totally Pwn CSS with Javascript for a good script on adding CSS rules dynamically. Also see Change style sheet for some of the theory on the subject.

此外,不要忘记,如果这是一个选项,您可以添加外部样式表的链接。例如,

Also, don't forget, you can add links to external stylesheets if that's an option. For example,

<script type="text/javascript">
  var link = document.createElement("link");
  link.setAttribute("rel","stylesheet");
  link.setAttribute("href","http://wherever.com/yourstylesheet.css");
  var head = document.getElementsByTagName("head")[0];
  head.appendChild(link);
</script>

注意:上面假设有一个 head 部分。

Caution: the above assumes there is a head section.

这篇关于如何写一个:悬停在内联CSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆