简体   繁体   English

当光标悬停在按钮上时更改背景颜色

[英]Change background color when the cursor is hovering on a button

I want the background color of the page to change when the mouse cursor is on a <button> but not if it's clicked.我希望页面的背景颜色在鼠标光标位于<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>

I copied that jQuery.fn from somewhere but I don't know how to make that if(cond) .我从某个地方复制了jQuery.fn但我不知道如何制作if(cond)

I want to check if mouse is hovering on the button, and if so, change the background color.我想检查鼠标是否悬停在按钮上,如果是,请更改背景颜色。

No jQuery needed for this if you're predominantly using native JS in your code.如果您主要在代码中使用原生 JS,则不需要 jQuery。 All you need is a mouseover event listener.您只需要一个mouseover事件侦听器。

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

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

$('#b1')
$( "#b1" ).mouseover(function() {
  $("div.background").css( "color", "red" );
});

Just add mouseenter and mouseleave event listeners to your button.只需将mouseentermouseleave事件侦听器添加到您的按钮即可。 No need for the overhead of jQuery for this solution.此解决方案不需要jQuery的开销。

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

I'm not sure what you mean by "not if it's clicked", but what about this?我不确定你所说的“如果它被点击就不行”是什么意思,但是这个呢?

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

Just add mouseenter and mouseleave.只需添加 mouseenter 和 mouseleave 即可。 It's the easiest and good solution.这是最简单和最好的解决方案。 Mousedown and up for moment of being clicked. Mousedown 和 up 是被点击的那一刻。

 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'; });
 <button id="b1" type="button">1</button>

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

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

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