简体   繁体   English

如何使用JQuery更改悬停复选框上的CSS样式

[英]How to Change CSS Styling on a Checkbox on Hover with JQuery

All I am trying to do is have my checkbox change it's background colour when you hover over it. 我要尝试做的就是将我的复选框悬停在其上方时更改其背景颜色。 However, this is made more complicated by the fact that the td input:checkbox is dynamically created. 但是,由于动态创建了td input:checkbox ,这一点变得更加复杂。 I have tried every conceivable approach and I can not get it right. 我尝试了所有可能的方法,但我做对了。 Here is the JQuery I have: 这是我拥有的JQuery:

/* snip */
    $('.table_products').on('click', 'td input:checkbox', function () {

    $("input:checkbox").hover(function(){
        $(this).css({"backgroundColor": "yellow"});
    }, function(){
        $(this).css({"backgroundColor": "white"});
    });
/* snip */

Maybe it isn't possible to change the color inside the checkbox square but you can trick as @gavgrif said. 也许无法更改复选框方框内的颜色,但是您可以像@gavgrif所说的那样欺骗。 I just hover when you pass on the checkbox (not on the entire td): 当您通过复选框(而不是整个td)时,我只是将鼠标悬停:

$("input[type='checkbox']").hover(function(){
  $(this).closest(".wrapper").css("background-color", "yellow");
}, function(){
  $(this).closest(".wrapper").css("background-color", "white");
});

Probably your selector was worng too (the correct way to declare is into []). 可能您的选择器也已磨损(正确的声明方式为[])。

I just wrapped the checkbox but you can use your current parent: 我只是包装了该复选框,但您可以使用当前的父级:

  <div class="wrapper">
    <input type="checkbox"/>
  </div>

Plunker: https://plnkr.co/edit/5V5cUysB9E0SYGMddFKl?p=preview 柱塞: https ://plnkr.co/edit/5V5cUysB9E0SYGMddFKl p = preview

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

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