2016-11-06 109 views
0

我有一个网站,我有选择框divs,当用户点击这些盒子时,它揭示了一个隐藏的内容div,也取代了选择框div的背景图像到一个蜱。这一切工作正常,但如果有人点击快速或点击选择框div的不同区域,选择框div的背景图像可能会显示tick,即使内容div再次隐藏。 我曾尝试过使用Google的各种方法,但大多数似乎都涉及到移动设备上的延迟。如果任何人有任何想法,请让我知道,我也知道我的代码可能不是最干净的。背景图像的大小为24kb,可以这样吗?我的代码如下:当点击替换CSS无法正常工作,快速点击

$(document).ready(function(){ 
 
    $(document).on('click','.checkbox',function(){ 
 
    target=$(this).data('target'); 
 
    $(target).slideToggle(400); 
 
    }); 
 
});
/* Option Boxes */ 
 
.checkbox{ 
 
    width:100%; 
 
    height: 50px; 
 
    position:relative; 
 
    float:left; 
 
    background-repeat:no-repeat; 
 
    background-image:url(../Images/Options/Template.png);     
 
    background-position: center center; 
 
    font-size:1.2em; 
 
    color:#000; 
 
    padding-top:2%; 
 
    text-align:center; 
 
    line-height:-100px; 
 
    margin-right:15%; 
 
    -webkit-touch-callout: none; 
 
    -webkit-user-select: none; 
 
    -khtml-user-select: none; 
 
    -moz-user-select: none; 
 
    -ms-user-select: none; 
 
    user-select: none; 
 
    cursor: default; 
 
} 
 
.myCheckbox input { 
 
    display:none; 
 
} 
 

 

 
.myCheckbox input:checked + .checkbox{ 
 
    width:100%; 
 
    height: 50px; 
 
    background-image: url("../Images/Options/Template-Tick.png");      
 
    color:#999; 
 
} 
 

 
/* Option Boxes */ 
 

 
.Content_Selection_Holder{ 
 
    position:relative; 
 
    float:left; 
 
    width:100%; 
 
    height:auto; \t 
 
} 
 
.Content_Selection_Boxes{ 
 
    position:relative; 
 
    float:left; 
 
    width:33%; 
 
    height:33%; \t 
 
    font-size:90%; 
 
} 
 
.Content_Answer_Holder{ 
 
    position:relative; 
 
    float:left; 
 
    width:100%; 
 
    height:auto; 
 
    margin-top:5%; \t 
 
} 
 
.Content_Answer_Box{ 
 
    position:relative; 
 
    float:left; 
 
    width:96%; 
 
    padding:2%; 
 
    height:auto; 
 
    display: none; 
 
}
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script> 
 
<div class="Content_Selection_Holder"> 
 

 
    <div class="Content_Selection_Boxes"><label class="myCheckbox"><input type="checkbox" name="test"/><div class="checkbox" data-target="#sb1"> Content1</div></div> 
 
    <div class="Content_Selection_Boxes"><label class="myCheckbox"><input type="checkbox" name="test"/><div class="checkbox" data-target="#sb2">Content2</div></div> 
 
     <div class="Content_Selection_Boxes"><label class="myCheckbox"><input type="checkbox" name="test"/><div class="checkbox" data-target="#sb3">Content3</div></div> 
 

 
     </div> <!-- Question Holder--> 
 

 
     <div class="Content_Answer_Holder"> 
 

 

 
     <div class="Content_Answer_Box" title="Some content 1" id="sb1"> 
 
      Some content 1 
 
     </div> 
 

 
     <div class="Content_Answer_Box" title="Content2" id="sb2"> 
 
      Some content 2   
 
     </div> 
 

 
     <div class="Content_Answer_Box" title="Content3" id="sb3"> 
 
      Some content 3   
 
     </div> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div>

回答

0

请参阅本之一:

  1. 在HTML中你错过了关闭标签标记

  2. ,所以我需要修复的CSS

  3. in js部分,这个想法是确保.checkbox触发输入类型= checkbox对应的点击事件。

$(document).ready(function(){ 
 
     $(document).on('click','.checkbox',function(){ 
 
      var self = $(this), 
 
      target=self.data('target');    
 
      self.prev('input').trigger('click');    
 
      $(target).slideToggle(400); 
 
     }); 
 
    });
/* Option Boxes */ 
 
.checkbox{ 
 
    width:100%; 
 
    height: 50px; 
 
    position:relative; 
 
    float:left; 
 
    background-repeat:no-repeat; 
 
    background-image:url(../Images/Options/Template.png);     
 
    background-position: center center; 
 
    font-size:1.2em; 
 
    color:#000; 
 
    padding-top:2%; 
 
    text-align:center; 
 
    line-height:-100px; 
 
    margin-right:15%; 
 
    -webkit-touch-callout: none; 
 
    -webkit-user-select: none; 
 
    -khtml-user-select: none; 
 
    -moz-user-select: none; 
 
    -ms-user-select: none; 
 
    user-select: none; 
 
    cursor: default; 
 
     } 
 
.myCheckbox+input { 
 
    display:none; 
 
      } 
 

 

 
input:checked + .checkbox{ 
 
    width:100%; 
 
    height: 50px; 
 
    background-image: url("../Images/Options/Template-Tick.png");      
 
    color:#999; 
 
      } 
 

 
/* Option Boxes */ 
 

 
.Content_Selection_Holder{ 
 
    position:relative; 
 
    float:left; 
 
    width:100%; 
 
    height:auto;  
 
} 
 
.Content_Selection_Boxes{ 
 
    position:relative; 
 
    float:left; 
 
    width:33%; 
 
    height:33%; 
 
    font-size:90%; 
 
} 
 
.Content_Answer_Holder{ 
 
    position:relative; 
 
    float:left; 
 
    width:100%; 
 
    height:auto; 
 
    margin-top:5%; 
 
} 
 
.Content_Answer_Box{ 
 
    position:relative; 
 
    float:left; 
 
    width:96%; 
 
    padding:2%; 
 
    height:auto; 
 
    display: none; 
 
}
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script> 
 

 
<div class="Content_Selection_Holder"> 
 

 
      <div class="Content_Selection_Boxes"> 
 
      <label class="myCheckbox"></label> 
 
      <input type="checkbox" name="test"/> 
 
      <div class="checkbox" data-target="#sb1"> Content1</div> 
 
      </div> 
 
      <div class="Content_Selection_Boxes"> 
 
      <label class="myCheckbox"></label><input type="checkbox" name="test"/><div class="checkbox" data-target="#sb2">Content2</div></div> 
 
      <div class="Content_Selection_Boxes"> 
 
      <label class="myCheckbox"></label> 
 
      <input type="checkbox" name="test"/><div class="checkbox" data-target="#sb3">Content3</div></div> 
 

 
      </div> <!-- Question Holder--> 
 

 
      <div class="Content_Answer_Holder"> 
 

 

 
      <div class="Content_Answer_Box" title="Some content 1" id="sb1"> 
 
      Some content 1 
 
      </div> 
 

 
      <div class="Content_Answer_Box" title="Content2" id="sb2"> 
 
      Some content 2   
 
      </div> 
 

 
      <div class="Content_Answer_Box" title="Content3" id="sb3"> 
 
Some content 3   
 
      </div>

+0

谢谢你这么多里兹之前,无法相信我错过了结束标签标记为下一次好!干杯 – mattyt81

0

从我能理解你想要的单击事件的复选框后触发已经改变了?让他们来匹配即使最终用户质量单击按钮

如果是这样,您将不得不在“更改”的情况下进行事件处理,以便在值发生更改后触发

.on('change',function() 

见例如这里http://codepen.io/anon/pen/MbYEmK

另外,作为RizkiDPrast指出besure检查你的HTML标记你问一个问题