jQuery Html 复选框:更改颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17432286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Html CheckBox : Change Color
提问by user2217039
During the developing of an .NET application I have came across a problem.
在 .NET 应用程序的开发过程中,我遇到了一个问题。
What I want to do, is to change the background color of a simple Html checkbox, so I have used the following HTML code:
我想要做的是更改一个简单的 Html 复选框的背景颜色,因此我使用了以下 HTML 代码:
<input type="checkbox" id="check1" style="background-color: yellow" />
This code works only with OPERA, and not with other browsers (Chrome, Firefox, Explorer)
此代码仅适用于 OPERA,不适用于其他浏览器(Chrome、Firefox、Explorer)
So i have used also Javascript code:
所以我也使用了 Javascript 代码:
document.getElementById("check1").style.backgroundColor = "yellow"
and JQuery sintax:
和 JQuery 语法:
$("#check1").css("background-color", "yellow")
but the result is the same.
但结果是一样的。
This code works if I use an HTML TextBox.
如果我使用 HTML 文本框,则此代码有效。
Can someone help me please ??
有人能帮助我吗 ??
回答by Mohammad Areeb Siddiqui
Wrap each checkbox into a div and then change the div's background-color.
将每个复选框包装到一个 div 中,然后更改 div 的背景颜色。
So it should be like this:
所以它应该是这样的:
<div style="background-color: yellow;">
<input type="checkbox" />
</div>
<div style="background-color: red;">
<input type="checkbox" />
</div>
<div style="background-color: green;">
<input type="checkbox" />
</div>
回答by bunjeeb
It will not work even in the new Opera, so I recommend to wrap it with a div container.
即使在新的 Opera 中它也不起作用,所以我建议用 div 容器包装它。
<div id="check1_Container">
<label for="check1">Check box 1</label>
<input type="checkbox" id="check1" style="background-color: yellow" />
</div>