javascript - onmouseover 覆盖了 :hover in CSS

标签 javascript html css

背景:我不确定发生了什么,但我有一个链接在 CSS 中关联了一个 a:hover。我还有一个图像,当用户滚动相同的文本链接时,它也应该改变颜色。如果用户首先将鼠标悬停在链接文本上,它会改变颜色没问题。但是一旦他们将鼠标悬停在图像上它仍然会改变颜色但鼠标悬停在文本上不再有效。

问题:与 onmouseover/onmouseout 关联的 JavaScript 能否取消 CSS a:hover 效果?

代码概述如下:

    <style type="text/css">
        a.cat_cartridge{
            text-decoration:none;
            color:#e77504;
            font:bold 12px arial;
        }

        a.cat_cartridge:hover {
            color:#cd1c1f;
            text-decoration:none;
        }
    </style>

     <div class="image-holder" style="width:150px;margin:0px auto;">
<a href="http://dev-store.url.com/product/serta-gel-king-title-/p810093914?pos=1:14&amp;Ntt=Mattresses">
    <img onmouseout="document.getElementById('mattresses_link').style.color='#e77504';" onmouseover="document.getElementById('mattresses_link').style.color='#cd1c1f';" src="http://qa-store.url.com/images/marketing/2016/091616-homepage-icon-mattresses.jpg" style="width:150px;height:150px;" />
    </a>
    </div>

    <div class="bottom-copy">
         <h3 style="text-align:center;">
             <a class="cat_cartridge" href="http://dev-store.url.com/product/serta-gel-king-title-/p810093914?pos=1:14&amp;Ntt=Mattresses" id="mattresses_link">Mattresses</a>
         </h3>
    </div>

最佳答案

你有一只流浪猫</div>第一个末尾的标记 <a>导致此问题的元素。删除它可以解决问题。修改后的标记:

<a href="http://dev-store.url.com/product/serta-gel-king-title-/p810093914?pos=1:14&amp;Ntt=Mattresses">        <img onmouseout="document.getElementById('mattresses_link').style.color='#e77504';" onmouseover="document.getElementById('mattresses_link').style.color='#cd1c1f';" src="http://qa-store.url.com/images/marketing/2016/091616-homepage-icon-mattresses.jpg" style="width:150px;height:150px;" /></a>

<div class="bottom-copy">
  <h3 style="text-align:center;">
    <a class="cat_cartridge" href="http://dev-store.url.com/product/serta-gel-king-title-/p810093914?           pos=1:14&amp;Ntt=Mattresses" id="mattresses_link">
      Mattresses
    </a>
  </h3>
</div>

关于javascript - onmouseover 覆盖了 :hover in CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39620923/

相关文章:

javascript - 对数组的副本执行 unshift 方法

javascript - 使用 styled-components 设置路由器链接的样式

javascript - 将 HTML 中的文本表单值输入到 Javascript 函数中

css - 连续 3 张图像在容器中居中

jquery - 单击时将容器中的内容向下推送,将内容加载到显示 Jquery 的空间中

javascript - 向下滚动页面时在侧边栏中显示部分标题(可能使用 BS scrollspy?)

html - CSS:我如何根据居中文本右对齐文本?

javascript - 使用 Ext.grid.Panel.reconfigure() 打破网格 RowEditing 插件

html - 如何让图像自动缩放(不使用断点)

css - 我的 Rails 样式表没有加载/工作