乐闻世界logo
搜索文章和话题

How can i write ahover in inline css

2个答案

1
2

In inline CSS, pseudo-class selectors such as 'a:hover' cannot be used directly within the 'style' attribute of HTML elements because the 'style' attribute is designed for inline styles only. Inline styles are primarily intended for styling individual elements, whereas ':hover' is a pseudo-class selector typically defined in internal or external style sheets.

However, if you wish to achieve a similar effect while restricted to inline styles, you can use JavaScript to dynamically modify the element's styles when the mouse hovers over it. Here is an example code snippet demonstrating how to simulate the a:hover effect using JavaScript:

html
<a href="#" id="hoverLink" onmouseover="hoverEffect(true)" onmouseout="hoverEffect(false)">Hover over me!</a> <script> function hoverEffect(isHovering) { var link = document.getElementById('hoverLink'); if (isHovering) { link.style.color = 'red'; // Color when mouse is hovering link.style.textDecoration = 'underline'; // Text decoration when mouse is hovering } else { link.style.color = 'initial'; // Color when mouse leaves link.style.textDecoration = 'none'; // Text decoration when mouse leaves } } </script>

In this example, when the mouse hovers over the link, hoverEffect(true) is invoked, setting the link's text color to red and adding an underline. When the mouse leaves the link, hoverEffect(false) is called, restoring the link's styles to their initial state. This method allows us to simulate the a:hover effect in inline CSS.

2024年6月29日 12:07 回复

You can achieve the same effect by using JavaScript to change styles within the onMouseOver and onMouseOut event handlers, although it is inefficient when modifying multiple elements:

html
<a href="abc.html" onMouseOver="this.style.color='#0F0'" onMouseOut="this.style.color='#00F'" >Text</a>

Run code snippet: Hide results

Expand snippet

Additionally, I cannot confirm whether this is valid in this context. You may need to use document.getElementById('idForLink') to toggle it.

2024年6月29日 12:07 回复

你的答案