简体   繁体   English

使用jQuery模拟复选框悬停效果

[英]Simulate checkbox hover effect with jQuery

How would I simulate the effect that happens to a checkbox when you hover over it using jQuery? 当您使用jQuery将鼠标悬停在复选框上时,我将如何模拟复选框中发生的效果? When you do this in Chrome and Firefox, the checkbox input is highlighted blue. 在Chrome和Firefox中执行此操作时,复选框输入将突出显示为蓝色。

To give a bit more context, I have a grid like so: 为了给出更多的上下文,我有一个像这样的网格:

<table>
    <tbody>
        <tr>
            <td><input type="checkbox" /></td>
            <td>this is a checkbox</td>
        </tr>
    </tbody>
</table

I want to make it that the checkbox is highlighted the default blue colour when a user hovers over the <tr> 我想让用户将鼠标悬停在<tr>时突出显示默认的蓝色

I realise I could put a label around this is a checkbox and reference the checkbox with the for attribute, however I would rather trigger this effect on hover over the <tr> . 我意识到我可以在它周围放置一个标签this is a checkbox并使用for属性引用复选框,但是我宁愿在悬停在<tr>上时触发此效果。

I'm a bit suspicious about using CSS to style the checkbox input itself as it seems a bit hacky. 我有点怀疑使用CSS来设置复选框输入本身的样式,因为它似乎有点hacky。

/edit after your edit: Some browsers (like Opera) seem to allow you to do things like padding-right:100px so that hovering the mouse near the checkbox would highlight the checkbox as well. /编辑后编辑:某些浏览器(如Opera)似乎允许您执行padding-right:100px以便将鼠标悬停在复选框附近也会突出显示该复选框。 You can try waste some time with that, but I'd highly recommend you do it with libraries. 你可以尝试浪费一些时间,但我强烈建议你用库来做。

That blueish hover sort of thing is from the operating system. 那种蓝色悬停的东西来自操作系统。 Only imaginable way would've been to focus it but obviously it doesn't work. 只有想象的方式才能集中注意力,但显然它不起作用。

You can do what we've all been doing for the past decade, turn checkboxes into hidden inputs and use images. 你可以做我们过去十年来一直在做的事情,将复选框变成隐藏的输入并使用图像。 Plenty of libraries out there. 那里有很多图书馆。

Here's one . 这是一个

I'd say your best bet for looks consistency is to replace the checkbox with your own crafted checkbox. 我说你看起来一致的最好的选择是用你自己制作的复选框替换复选框。 I extracted and edited this from a plugin I made a while ago. 我从一段时间前制作的插件中提取并编辑了这个。

Demo: http://jsfiddle.net/elclanrs/jTteE/ 演示: http //jsfiddle.net/elclanrs/jTteE/

html: HTML:

<label><input type="checkbox">Hey</label>

css: CSS:

input[type="checkbox"] {
    position: absolute;
    margin-left: -9999px;
}
.check {
    position: relative;
    display: inline-block;
    vertical-align: top;
    margin-right: .5em;
    width: 1.1em;
    height: 1.1em;
    background: #ddd;
}
.check:hover {
    background: pink;
}
.check.checked {
    background: red;
}
.check.checked:after {
    content: "\2713";
    position: absolute;
    font-size: 22px;
    font-weight: bold;
    line-height: 16px;
    top: 0;
    z-index: 999;
    color: white;
}

js: JS:

// Check
$('input:checkbox').each(function() {
    $(this).before('<span class="check"></span>');
    if ($(this).is(':checked')) {
        $(this).prev('span').addClass('checked');
    }
}).change(function() {
    $(this).prev('span').toggleClass('checked');
});

Mr. ajbeaven, meet the <label> tag! ajbeaven先生,见到<label>标签! ;-) ;-)

ANSWER: http://jsfiddle.net/wh5N9/76/ 答案: http //jsfiddle.net/wh5N9/76/
No javascript, just HTML ... 没有javascript,只有HTML ...

Any mouse action done to a label will reflect on its checkbox (this includes hovering, and clicking). 标签执行的任何鼠标操作都将反映在其复选框上 (包括悬停和单击)。

The <label> tag defines a label for an element. <label>标签定义元素的标签。

The <label> element does not render as anything special for the user. <label>元素不会呈现为用户特殊的内容。 However, it provides a usability improvement for mouse users , because if the user clicks on the text within the <label> element, it toggles the control. 但是,它为鼠标用户提供了可用性改进 ,因为如果用户单击<label>元素中的文本,它将切换控件。

The for attribute of the <label> tag should be equal to the id attribute of the related element to bind them together. <label>标签的for属性应该等于相关元素的id属性,以将它们绑定在一起。

You would just need to match the checkbox id inside the label's "for" attribute, as seen below: 您只需要匹配标签的“for”属性中的复选框ID ,如下所示:

<input type="checkbox" id="YAHOO"><label for="YAHOO">whatever whatever</label>

You can have as many labels for a checkbox, and style them with a fixed width and height so it can be as high or as wide as you want it to be, and fit right inside that tr of yours. 您可以为复选框添加任意数量的标签,并使用固定的宽度和高度对其进行样式设置,使其可以根据您的需要设置为高或宽,并且适合您的tr

I was able to target checkbox clicking on a tr click using jQuery, but not simulate its hovering effect. 我能够使用jQuery定位复选框单击tr单击,但不能模拟其悬停效果。 Thus, I had to fall back on the <label> . 因此,我不得不退回<label>

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

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