【问题标题】:Change background color when the cursor is hovering on a button当光标悬停在按钮上时更改背景颜色
【发布时间】:2018-05-15 16:07:23
【问题描述】:

我希望当鼠标光标位于 <button> 上时页面的背景颜色发生变化,但如果单击则不会。

jQuery.fn.mouseIsOver = function () {
  if($(this[0]).is(":hover"))
  {
    return true;
  }
  return false;
}; 		
 
if($("b1").mouseIsOver()===true)
{
  document.body.style.backgroundColor = "#AA0000";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="b1", type="button">1</button>

我从某处复制了 jQuery.fn,但我不知道如何制作 if(cond)。

我想检查鼠标是否悬停在按钮上,如果是,请更改背景颜色。

【问题讨论】:

  • 这个不起作用的原因是if语句只执行了一次。为了不断检查按钮是否悬停,您需要使用事件监听器。
  • 请检查您使用的答案旁边的绿色 check 符号

标签: javascript jquery html button mouse


【解决方案1】:

如果你想定位 id,你必须以 # (dieze) 开始选择器

$('#b1')

【讨论】:

    【解决方案2】:
    $( "#b1" ).mouseover(function() {
      $("div.background").css( "color", "red" );
    });
    

    【讨论】:

      【解决方案3】:

      如果您主要在代码中使用原生 JS,则不需要 jQuery。您只需要一个mouseover 事件监听器。

      document.getElementById('b1').addEventListener('mouseover', () => {
        document.body.style.backgroundColor = '#aa0000';
      });
      &lt;button id="b1", type="button"&gt;1&lt;/button&gt;

      【讨论】:

        【解决方案4】:

        我不确定你所说的“如果它被点击了”是什么意思,但是这个呢?

        <button id="theButton">Press Me</button>
        
        <script>
        $('#theButton')
            .on('mouseenter', function(event) {
                $('body').css('background-color', 'blue');
            })
            .on('mouseleave', function(event) {
                $('body').css('background-color', 'yellow');
            });
        </script>
        

        【讨论】:

          【解决方案5】:

          只需将mouseenter 和mouseleave 事件侦听器添加到您的按钮。这个解决方案不需要jQuery 的开销。

          document.getElementById('button').addEventListener('mouseenter', () => {
            document.body.style.backgroundColor = 'yellow';
          });
          
          document.getElementById('button').addEventListener('mouseleave', () => {
            document.body.style.backgroundColor = 'white';
          });
          &lt;button id="button"&gt;Button&lt;/button&gt;

          【讨论】:

            【解决方案6】:

            只需添加 mouseenter 和 mouseleave。这是最简单和最好的解决方案。鼠标向下和向上用于被点击的那一刻。

            document.getElementById('b1').addEventListener('mouseenter', () => {
              document.body.style.backgroundColor = '#A00';
            });
            
            document.getElementById('b1').addEventListener('mouseleave', () => {
              document.body.style.backgroundColor = '#FFF';
            });
            
            
            document.getElementById('b1').addEventListener('mouseup', () => {
              document.body.style.backgroundColor = '#A00';
            });
            
            document.getElementById('b1').addEventListener('mousedown', () => {
              document.body.style.backgroundColor = '#FFF';
            });
            &lt;button id="b1" type="button"&gt;1&lt;/button&gt;

            【讨论】:

              【解决方案7】:

              .button{
                 cursor:pointer;
              }
              .button:hover{
                  background:red;
              }

              【讨论】:

              • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助、质量更好,并且更有可能吸引投票。
              • 这是通过使用 CSS 完成的,我们可以在悬停大括号中包含其他属性
              猜你喜欢
              • 2019-12-06
              • 1970-01-01
              • 2017-12-15
              • 2011-04-04
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-04-12
              相关资源
              最近更新 更多