用于锚标记和悬停的内联CSS

Joh*_*n R 9 css

我的印象是在悬停时更改锚标记可以这样做:

a:hover {background: #FFDD00;}
a:hover {color: #AAAAAA;}
Run Code Online (Sandbox Code Playgroud)

如我错了请纠正我.

现在,由于一些令人费解的原因,我无法将该代码放在样式表中,我必须将其放在实际的HTML中.我该怎么办?

<a href="..." style="___???___">...</a> 
Run Code Online (Sandbox Code Playgroud)

thi*_*dot 10

没有办法做到这一点.

内联CSS无法触及伪类等:hover.

我猜你想要这样做的原因是因为你只能编辑<body>HTML(无论出于何种原因).你可以做的是添加一个style元素:

<style>
a:hover {
    background: #FFDD00;
    color: #AAAAAA;
}
<style>

<a href="#">...</a>
Run Code Online (Sandbox Code Playgroud)

style外面有一个元素<head>是无效的HTML,但(至关重要)它在所有浏览器中都有效.


nin*_*ipt 6

如果你不能将你的悬停CSS扔进一个标签,那么处理这个问题的最好方法就是使用JavaScript.我通常不会称这是一个好方法,但听起来你的双手被绑在这里.

<a href="..."
   onmouseover="this.style.backgroundColor='#ffdd00';this.style.color='#aaaaaa'"
   onmouseout="this.style.backgroundColor='transparent';this.style.color='inherit'">
...
</a>
Run Code Online (Sandbox Code Playgroud)

希望对你有用!


小智 5

在一个旧的论坛上发现了它,似乎效果很好:)

<a href="###" style="text-decoration: none;" onmouseover="this.style.textDecoration = 'underline'" onmouseout="this.style.textDecoration = 'none'">###</a>
Run Code Online (Sandbox Code Playgroud)