javascript - 无法使复选框自定义图像显示复选标记

标签 javascript jquery html css checkbox

我添加了 CSS 来自定义复选标记框上的图像,单击它时应该会显示带有复选标记的图像。然而,经测试它不起作用。

我使用了这个 stack overflow 中的 CSS 和 HTML为多对复选框实现自定义图像。 Sprite 的每个图标都有 50 像素的高度堆叠。

不能 100% 确定我哪里出错导致它不起作用,我的代码没有选中和取消选中复选框可能有什么问题?

fiddle

<html><head>
    <style>
        @font-face {
    font-family: 'Scribble Box'; /*a name to be used later*/
    src: url('/scribbleboxdemo.ttf'); /*URL to font*/
}
        .scribblebox{
    font-family: 'Scribble Box';
    font-size: 50px;
}
.overlay {
  display: none;
}
.right {
  position: absolute;
  right: 0px;
  width: 300px;
  border: 3px solid #8AC007;
  padding: 10px;
}
#map {
  position: relative;
  /*right: -780px; removed for demo purposes */
  width: 452px;
  height: 344px; 
    float: left;
  background: url(//preview.jesybyqcev4e7b9xn83mzparyiafw29nwvpl11qsrsmunmi.box.codeanywhere.com/BLANK-COMPUTER-SCREEN.png) no-repeat;
}
#station_A {
  top: 8%; /* whatever you like */
  left: 5%; /* whatever you like */
  position: absolute;
}
#station_B {
  top: 45%; /* whatever you like */
  left: 15%; /* whatever you like */
  position: absolute
}
        #station_C{
  top: 8%; /* whatever you like */
  left: 5%; /* whatever you like */
  position: absolute;
}
.hover {
  color: green
}
        #wrapper {
  width:4000px;
  clear:both;
}
#first {
  border: 3px solid black;
  width:300px;
  float:left;
}
#second {
  border: 3px solid black;
  width:300px;
  float:left;
}
#third {
  border: 3px solid black;
  width:200px;
  float:left;
}
input[type=checkbox]{  display:none; }

input[type=checkbox] + label{
    background-image: url('/checkmarkboxes.png');
    display: block;
    height: 50px;
    padding: 0;
    width: 48px; }


#lightblue + #lightblue_s{ background-position: 0 50; }
#lightblue:checked + #lightblue_s{ background-position: 0 -50px; }
</style>


<div id="wrapper">

<div id="map">
  <span id="station_A" class="overlay"><img src="/Bandana_top.png" /></span>
  <span id="station_B" class="overlay">Highlight image here.</span>
  <span id="station_C" class="overlay"><img src="Bandana_top.png" class="filter-tint" data-pb-tint-opacity="0.5" data-pb-tint-colour="#8fc0ff"></span>
</div>
<p>
            <div id="first">
            <h2>
                Choose a Color
            </h2>
             <input type='checkbox' name='methods' value='valuable' id="lightblue"/><label id="lightblue_s" for="lightblue"></label>
    </div>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>'
  <script type="text/javascript" src="common.js"></script>
  <script type="text/javascript" src="paintbrush.js"></script>
  <script>
 $(document).ready(function() {
   $("input[type='checkbox']").change(function() {
     var state = $(this).val();
     $("#" + state).toggleClass("overlay");
   });
   $('#checkbox1').change(function() {
     if (this.checked) $('#map').fadeIn('slow');
     else $('#map').fadeOut('slow');

   });
 });
    </script>

Updated fiddle

最佳答案

我想通了,似乎当我默认回到原始代码然后为复选标记添加自定义图像时一切正常。所以我假设我搞砸了编码。

<style>
    .overlay {
  display: none;
}
.right {
  position: absolute;
  right: 0px;
  width: 300px;
  border: 3px solid #8AC007;
  padding: 10px;
}
#map {
  position: relative;
  /*right: -780px; removed for demo purposes */
  width: 452px;
  height: 344px; 
  background: url(//preview.jesybyqcev4e7b9xn83mzparyiafw29nwvpl11qsrsmunmi.box.codeanywhere.com/BLANK-COMPUTER-SCREEN.png) no-repeat;
}
#station_A {
  top: 8%; /* whatever you like */
  left: 5%; /* whatever you like */
  position: absolute;
}
#station_B {
  top: 45%; /* whatever you like */
  left: 15%; /* whatever you like */
  position: absolute
}
.hover {
  color: green
}
    input[type=checkbox]{  display:none; }

input[type=checkbox] + label{
    background-image: url('checkmarkboxes.png');
    display: block;
    height: 50px;
    padding: 0;
    width: 48px; }

#lightblue + #lightblue_s{ background-position: 0 0; }
#lightblue:checked + #lightblue_s{ background-position: 0 -50px; }


</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="map">
  <span id="station_A" class="overlay"><img style="background:url(//preview.jesybyqcev4e7b9xn83mzparyiafw29nwvpl11qsrsmunmi.box.codeanywhere.com/BLANK-COMPUTER-SCREEN.png)" src="//lorempixel.com/270/240" /></span>
  <span id="station_B" class="overlay">Highlight image here.</span>
</div>
<p>
  <h2>Choose a Shirt</h2>

  <form>
    <input type="checkbox" name="image" value="station_A" id="lightblue"><label id="lightblue_S" for="lightblue"></label>
    <br>
    <input type="checkbox" name="image" value="station_B">Station Beta
    <input type="checkbox" name="image" value="bandanna" id="checkbox1" />Bandanna
  </form>
</p>
<script>
    $(document).ready(function() {
   $("input[type='checkbox']").change(function() {
     var state = $(this).val();
     $("#" + state).toggleClass("overlay");
   });
   $('#checkbox1').change(function() {
     if (this.checked) $('#map').fadeIn('slow');
     else $('#map').fadeOut('slow');

   });
 });
</script>

<

关于javascript - 无法使复选框自定义图像显示复选标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31545691/

相关文章:

jquery - 使用部分 View 的客户端验证

javascript - document.ready 中的调用函数不起作用

jquery - 为什么是:contains(text) select all <i> tags?

javascript - 更改导航栏中的事件类别

javascript - 如何只打印选定的 HTML 元素?

javascript - 奇怪的javascript变量转储

javascript - Div 容器隐藏在另一个 div 后面,即使 z-index 为 999

javascript - 通过单词检查两个文本的相似度?

javascript - 欧洲号码正则表达式

javascript - 在 JavaScript 中获取对象属性