简体   繁体   English

onmouseover 覆盖了 CSS 中的 a:hover

[英]onmouseover is overridding a:hover in CSS

Background : I am not sure what is going on, but I have a link that has an a:hover associated with it in the CSS.背景:我不确定发生了什么,但我有一个链接,在 CSS 中它有一个a:hover与之关联。 I also have an image that when the user rolls over that same link of text should also change color.我还有一张图片,当用户翻过相同的文本链接时,它也应该改变颜色。 If the user mouses over the link text first it changes color no problem.如果用户首先将鼠标悬停在链接文本上,它会改变颜色没问题。 but as soon as they mouse over the image it still changes colors but the mouse over the text no longer works.但是一旦他们将鼠标悬停在图像上,它仍然会改变颜色,但将鼠标悬停在文本上时不再起作用。

Question: Could the JavaScript associated with the onmouseover / onmouseout cancel out the CSS a:hover effect?问题:onmouseover / onmouseout相关联的 JavaScript 能否取消 CSS a:hover效果?

Code outlined below:代码概述如下:

    <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>

You have a stray </div> tag at the end of the first <a> element which is causing this issue.在导致此问题的第一个<a>元素的末尾有一个杂散的</div>标记。 Removing this will solve the problem.删除它可以解决问题。 Amended markup:修正标记:

<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>

Could the JavaScript associated with the onmouseover/onmouseout cancel out the CSS a:hover effect?onmouseover/onmouseout相关的 JavaScript 能否取消 CSS a:hover 效果?

Yes是的

 $( "div" ).mouseover(function() { // uncomment and run again to see how mouseover overrides hover // $("div").css({ 'background-color' : 'white'}) });
 div { width: 300px; height: 400px; background-color: red; } div:hover { background-color: green; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> </div>

Remove your onmouseover and onmouseout from HTML and simply add this to CSS从 HTML 中删除您的onmouseoveronmouseout并将其添加到 CSS

    .image-holder:hover a.cat_cartridge{
        color:#cd1c1f;
        text-decoration:none;
    }

 <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; } .image-holder:hover a.cat_cartridge{ 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 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> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM