简体   繁体   English

ton&gt;将鼠标悬停在<a>标签上以更改标签内的按钮</a><a>颜色</a>

[英]ton>Hover over an <a> tag to change a button color inside the <a> tag

My webpage is constructed as:我的网页构造为:

   <a class="hoverMe" href="">   
      <div class="somediv1">
         <div class="somediv2">
           <div class="somediv3">
              <button class="changeColor"></button>
           </div>
          </div>   
      </div>
   </a> 


   <a class="hoverMe" href="">   
      <div class="somediv1">
         <div class="somediv2">
           <div class="somediv3">
              <button class="changeColor"></button>
           </div>
          </div>   
      </div>
   </a> 

I want to be able to change the color of the button inside the particular <a> </a> that's being hovered over.我希望能够更改悬停在特定<a> </a>内的按钮的颜色。 How should I write the javascript to do it?我应该如何编写 javascript 来做到这一点? Thanks so much for the help!非常感谢你的帮忙!

First at all, you only need a button to fire an JS onclick event.首先,您只需要一个按钮来触发 JS onclick 事件。 In every other case you use a div to style a "button".在其他所有情况下,您都使用 div 来设置“按钮”的样式。 Also a button is not an empty tag and therefor needs a closing tag <button>Text</button>按钮也不是空标签,因此需要一个结束标签<button>Text</button>

Just like this:像这样:

 a { text-decoration: none; color: black; } a div { border: 1px solid black; width: min-content; white-space: nowrap; padding: 5px; }
 <a href=""><div>I'm a Link-Button</div></a>

to change the color during hover, you dont need JS.在hover期间改变颜色,你不需要JS。 You can simply use the :hover pseudo selector like this:您可以像这样简单地使用:hover伪选择器:

 a { text-decoration: none; color: black; } a div { border: 1px solid black; width: min-content; white-space: nowrap; padding: 5px; } a:hover div { background-color: red; }
 <a href=""><div>I'm a Link-Button</div></a>

As you insist on using your invalid HTML and seem not to understand the use of:hover the same for your code:由于您坚持使用无效的 HTML 并且似乎不理解以下代码的用法:hover 与您的代码相同:

 .hoverMe:hover.changeColor { background-color: red; }
 <a class="hoverMe" href=""> <div class="somediv1"> <div class="somediv2"> <div class="somediv3"> <button class="changeColor">Button 1</button> </div> </div> </div> </a> <a class="hoverMe" href=""> <div class="somediv1"> <div class="somediv2"> <div class="somediv3"> <button class="changeColor">Button 2</button> </div> </div> </div> </a>

This is a pretty basic question checking docs usually can help you.这是一个非常基本的问题,检查文档通常可以帮助您。 Check this out看看这个

The use of :hover pseudo selector will help you achieve what you want. :hover伪选择器的使用将帮助您实现您想要的。 In our example we see the a:hover action which enables the hover color change.在我们的示例中,我们看到了a:hover操作,它启用了 hover 颜色更改。

 a:hover { background-color: yellow; } a { font: bold 11px Arial; text-decoration: none; background-color: #EEEEEE; color: #333333; padding: 2px 6px 2px 6px; border-top: 1px solid #CCCCCC; border-right: 1px solid #333333; border-bottom: 1px solid #333333; border-left: 1px solid #CCCCCC; }
 <a href="https://www.w3schools.com">w3schools.com</a> <a href="https://www.wikipedia.org">wikipedia.org</a> <p><b>Note:</b> The:hover selector style links on mouse-over.</p>

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

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