我有一个情景需要编写内联CSS代码,我想在锚点上应用悬停样式。
如何在HTML的style属性中使用a:hover
实现?
例如,在HTML电子邮件中无法可靠地使用CSS类。
我有一个情景需要编写内联CSS代码,我想在锚点上应用悬停样式。
如何在HTML的style属性中使用a:hover
实现?
例如,在HTML电子邮件中无法可靠地使用CSS类。
<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部分。
style="{color:green;} :hover { color: red; }"
进行测试,火狐浏览器可以将链接颜色变为绿色,但忽略了:hover(悬停)效果。谷歌浏览器两个都忽略了。继续测试将毫无意义。 - Jonathan Fingland如果您需要更改多个元素,则可以使用JavaScript在onMouseOver
和onMouseOut
参数中更改样式,尽管这种方法效率极低:
<a href="abc.html"
onMouseOver="this.style.color='#0F0'"
onMouseOut="this.style.color='#00F'" >Text</a>
另外,我不确定在这种情况下this
是否有效。你可能需要将它替换为document.getElementById('idForLink')
。
<div onMouseOver="this.style.backgroundColor='#F8F8F8'" onMouseOut="this.style.backgroundColor='#FFFFFF'"> ...
- mxro你曾经可以这样做在过去的某个时候。但是现在(根据同一标准的最新修订,即候选推荐),你不能这样做。
<a href="http://www.w3.org/" style="{color: #900} :link {background: #ff0} :visited {background: #fff} :hover {outline: thin red solid} :active {background: #00f}">...</a>
,但它没有起作用。 - Amr Elgarhy您无法完全实现您所描述的功能,因为 a:hover
是选择器的一部分,而不是 CSS 规则。样式表有两个组成部分:
selector {rules}
内联样式只有规则,选择器隐含为当前元素。
选择器是一种表达式语言,用于描述在类似XML的文档中匹配元素的一组条件。
但是,你可以接近实现,因为一个style
集合在技术上几乎可以放在任何地方:
<html>
<style>
#uniqueid:hover {do:something;}
</style>
<a id="uniqueid">hello</a>
</html>
<html> </html>
标记下面。 - user379888:hover {
color: var(--hover-color);
}
<a style="--hover-color: green">
Library
</a>
考虑在选择器之外使用属性或类(例如[hover-color]:hover
),以允许与其他低特异性的悬停颜色更改规则共存(例如CSS重置或一些元素使用默认样式)。
如果你确实需要内联代码,这是可以做到的。我需要用它来制作一些悬停按钮,方法如下:
.hover-item {
background-color: #FFF;
}
.hover-item:hover {
background-color: inherit;
}
<a style="background-color: red;">
<div class="hover-item">
Content
</div>
</a
a)添加内联样式
document.head.insertAdjacentHTML('beforeend', '<style>#mydiv:hover{color:red;}</style>');
b) 或者略微困难的方法 - 添加 "mouseover" document.getElementById("mydiv").onmouseover= function(e){this.className += ' my-special-class'; };
document.getElementById("mydiv").onmouseleave= function(e){this.className = this.className.replace('my-special-class',''); };
注意:JavaScript中的多字样式(例如font-size
)书写时需要连写:
element.style.fontSize="12px"
这是最好的代码示例:
<a
style="color:blue;text-decoration: underline;background: white;"
href="http://aashwin.com/index.php/education/library/"
onmouseover="this.style.color='#0F0'"
onmouseout="this.style.color='#00F'">
Library
</a>
主持人建议:保持你的关注点分离。
在IT技术中,关注点分离是一种设计原则,旨在将不同方面的功能分开处理,从而提高代码的可维护性和可扩展性。HTML
<a
style="color:blue;text-decoration: underline;background: white;"
href="http://aashwin.com/index.php/education/library/"
class="lib-link">
Library
</a>
JS
const libLink = document.getElementsByClassName("lib-link")[0];
// The array 0 assumes there is only one of these links,
// you would have to loop or use event delegation for multiples
// but we won't go into that here
libLink.onmouseover = function () {
this.style.color='#0F0'
}
libLink.onmouseout = function () {
this.style.color='#00F'
}
当前版本的CSS不支持内联伪类声明(尽管据我所知,它可能会在未来版本中出现)。
目前,你最好的选择可能是直接在要样式化的链接上方定义一个样式块:
<style type="text/css">
.myLinkClass:hover {text-decoration:underline;}
</style>
<a href="/foo" class="myLinkClass">Foo!</a>
如已指出,您无法为悬停设置任意内联样式,但是您可以使用以下CSS在适当的标记中更改悬停光标的样式:
style="cursor: pointer;"