鱼C论坛

 找回密码
 立即注册
查看: 1934|回复: 3

[已解决]有关鼠标放到按钮上实现高亮的问题

[复制链接]
发表于 2020-4-3 15:50:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我想按照书本上的方式利用Event对象的方式实现两个按钮鼠标放上去时高亮,移开时还原的效果。但是我发现鼠标放到按钮上时,按钮上登录两个字的背景色却并没有高亮显示,鼠标放到登录上面登录两个字的颜色高亮了其他地方却没有高亮,是有什么地方不对吗。之前用button标签的属性实现的时候是没有这问题的
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="utf-8">

  5.                 <link rel="icon" href="goo.jpg">
  6.                 <title>在线多人聊天室</title>

  7.                 <style>  
  8.                         body{
  9.                                 background-image: url("hyper_beast1.jpg");
  10.                                 background-size: cover;
  11.                                 background-position: 0px 0px;
  12.                                 background-repeat: no-repeat;
  13.                         }        
  14.                         h2{
  15.                                 color: white;
  16.                         }
  17.                         .login{
  18.                                 height: 400px;
  19.                                 width: 400px;
  20.                                 background-color: rgba(255, 255, 255, 0.75);
  21.                                 border-width: 2px;
  22.                                 border-color: black;
  23.                                 border-style: solid;
  24.                                 border-radius: 15px;
  25.                         }

  26.                         #log1{
  27.                                 background: green;
  28.                                 height: 40px;
  29.                                 width: 300px;
  30.                                 border-radius: 5px;
  31.                                 font-size: 20px;
  32.                         }

  33.                         #register1{
  34.                                 background: green;
  35.                                 height: 40px;
  36.                                 width: 300px;
  37.                                 border-radius: 5px;
  38.                                 font-size: 20px;
  39.                         }

  40.                 </style>

  41.         </head>

  42.         <body>
  43.                 <h2><center>在线多人聊天登录:</center></h2>
  44.                 <br><br>
  45.                 <center>
  46.                         <div class="login">
  47.                                 <form class="login1" method="POST"></form>
  48.                                         <br><br><br><br>
  49.                                         <label>账户:</label> <input id="account" autofocus> <br><br>
  50.                                         <label>密码:</label> <input type="password"> <br><br><br><br><br><br>

  51.                                         <button id="log1" type="submit" value=""><b>登录</b></button>
  52.                                         <br><br>
  53.                                         <button id="register1" type="button"><b>注册</b></button>
  54.                                 </form>
  55.                         </div>
  56.                 </center>
  57.                
  58.                 <script>
  59.             document.getElementById("register1").onclick = function()
  60.                         {

  61.                 document.location.assign("February.html");
  62.             }

  63.                         var elems=document.getElementsByTagName('button');
  64.                         for (var i=0;i<elems.length;i++)
  65.                         {
  66.                                 elems[i].onmouseover=HandleOnButton;
  67.                                 elems[i].onmouseout=HandleOutButton;
  68.                         }


  69.                         function HandleOnButton(e)
  70.                         {
  71.                                 e.target.style.background='#28FF28';
  72.                         }

  73.                         function HandleOutButton(e)
  74.                         {
  75.                                 e.target.style.background='green';
  76.                         }

  77.         </script>


  78.         </body>

  79. </html>
复制代码
最佳答案
2020-4-3 19:32:17
Junpei 发表于 2020-4-3 19:22
你好,你的意思是target不仅会选中button也会选中它的子标签是吗,我试了一下去掉b标签确实可以

onmouseover 对<button>和<b>这两个盒子分别起作用,你用console.log(elems),DOM层次中<b>依旧是一个子节点,应该是js 的事件冒泡起作用,所以子元素onmouseover父元素 onmouseout
q1.png
q2.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-4-3 18:20:00 | 显示全部楼层
有了buttton 里面的<b>去掉不就可以啦~
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="utf-8">

  5.                 <link rel="icon" href="goo.jpg">
  6.                 <title>在线多人聊天室</title>

  7.                 <style>  
  8.                         body{
  9.                                 background-image: url("hyper_beast1.jpg");
  10.                                 background-size: cover;
  11.                                 background-position: 0px 0px;
  12.                                 background-repeat: no-repeat;
  13.                         }        
  14.                         h2{
  15.                                 color: white;
  16.                         }
  17.                         .login{
  18.                                 height: 400px;
  19.                                 width: 400px;
  20.                                 background-color: rgba(255, 255, 255, 0.75);
  21.                                 border-width: 2px;
  22.                                 border-color: black;
  23.                                 border-style: solid;
  24.                                 border-radius: 15px;
  25.                         }

  26.                         #log1{
  27.                                 background: green;
  28.                                 height: 40px;
  29.                                 width: 300px;
  30.                                 border-radius: 5px;
  31.                                 font-size: 20px;
  32.                         }

  33.                         #register1{
  34.                                 background: green;
  35.                                 height: 40px;
  36.                                 width: 300px;
  37.                                 border-radius: 5px;
  38.                                 font-size: 20px;
  39.                         }

  40.                 </style>

  41.         </head>

  42.         <body>
  43.                 <h2><center>在线多人聊天登录:</center></h2>
  44.                 <br><br>
  45.                 <center>
  46.                         <div class="login">
  47.                                 <form class="login1" method="POST"></form>
  48.                                         <br><br><br><br>
  49.                                         <label>账户:</label> <input id="account" autofocus> <br><br>
  50.                                         <label>密码:</label> <input type="password"> <br><br><br><br><br><br>

  51.                                         <button id="log1" type="submit" value="">登录</button>
  52.                                         <br><br>
  53.                                         <button id="register1" type="button">注册</button>
  54.                                 </form>
  55.                         </div>
  56.                 </center>
  57.                
  58.                 <script>
  59.             document.getElementById("register1").onclick = function()
  60.                         {

  61.                 document.location.assign("February.html");
  62.             }

  63.                         var elems=document.getElementsByTagName('button');
  64.                         for (var i=0;i<elems.length;i++)
  65.                         {
  66.                                 elems[i].onmouseover=HandleOnButton;
  67.                                 elems[i].onmouseout=HandleOutButton;
  68.                         }


  69.                         function HandleOnButton(e)
  70.                         {
  71.                                 e.target.style.background='#28FF28';
  72.                         }

  73.                         function HandleOutButton(e)
  74.                         {
  75.                                 e.target.style.background='green';
  76.                         }

  77.         </script>


  78.         </body>

  79. </html>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-3 19:22:39 | 显示全部楼层
dlnb526 发表于 2020-4-3 18:20
有了buttton 里面的去掉不就可以啦~

你好,你的意思是target不仅会选中button也会选中它的子标签是吗,我试了一下去掉b标签确实可以
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-3 19:32:17 | 显示全部楼层    本楼为最佳答案   
Junpei 发表于 2020-4-3 19:22
你好,你的意思是target不仅会选中button也会选中它的子标签是吗,我试了一下去掉b标签确实可以

onmouseover 对<button>和<b>这两个盒子分别起作用,你用console.log(elems),DOM层次中<b>依旧是一个子节点,应该是js 的事件冒泡起作用,所以子元素onmouseover父元素 onmouseout
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-2 07:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表