使用 jQuery 重定向复选框
Redirect a checkbox with jQuery
所以我有一个很酷的开关盒的html和css,我将它用作我网站上的语言开关。但是,由于我是 JavaScript 的初学者,因此缺少可以执行重定向操作的处理程序。
这是我到目前为止的工作。问题出在 JavaScript 上。
$(".flag-switch input").click(function(){$(this).is(":checked")?setTimeout(function(){window.location.href="en/"},250):setTimeout(function(){window.location.href="../"},250)}
.flag-switch {
position: relative;
right: -30px;
top: 4px;
outline: 0;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
-webkit-perspective: 1000;
-webkit-backface-visibility: hidden;
-webkit-transform: translate3d(0, 0, 0);
-webkit-tap-highlight-color: transparent;
width: 60px;
height: 30px;
margin: 5px auto;
}
.flag-switch:before, .flag-switch:after {
position: absolute;
color: white;
font-family: 'Raleway', sans-serif;
top: 8px;
font-size: 13px;
font-weight: 700;
}
.flag-switch:before {
left: -19px;
content: attr(data-first-lang);
}
.flag-switch:after {
right: -17px;
content: attr(data-second-lang);
}
.flag-switch input {
display: none;
}
.flag-switch input + label {
display: block;
position: absolute;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
width: 60px;
}
.flag-switch input + label:before, .flag-switch input + label:after {
content: "";
position: absolute;
border-radius: 30px;
-webkit-transition: all 0.25s ease-in-out;
transition: all 0.25s ease-in-out;
}
.flag-switch input + label:before {
height: 30px;
width: 60px;
background-color:#EE4B53 ;
}
.flag-switch input + label:after {
top: 3px;
left: 3px;
border: 2px solid #DFDFDF;
width: 24px;
height: 24px;
background: #eb3 url(http://i.imgur.com/mYEJa6E.png) -26px center;
transform: translate(30px, 0);
-webkit-transform: translate(30px, 0);
}
.flag-switch input:checked + label:after {
background: #000233 url(http://i.imgur.com/mYEJa6E.png) -2px center;
-webkit-transition: all 0.25s ease-in-out;
transition: all 0.25s ease-in-out;
-webkit-transform: translate(0, 0);
transform: translate(0, 0);
}
.flag-switch input:checked + label:before {
background-color: #1FA9D6;
}
<div class="flag-switch" data-first-lang="GR" data-second-lang="EN">
<input type="checkbox" id="check1" checked>
<label for="check1"></label>
</div>
相同的
Javascript版本:
document.querySelector('#check1').addEventListener('change', function(){
if(this.checked){
setTimeout(function(){window.location.href="en/"},250);
}
else{
setTimeout(function(){window.location.href="../"},250);
}
});
.flag-switch {
position: relative;
right: -30px;
top: 4px;
outline: 0;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
-webkit-perspective: 1000;
-webkit-backface-visibility: hidden;
-webkit-transform: translate3d(0, 0, 0);
-webkit-tap-highlight-color: transparent;
width: 60px;
height: 30px;
margin: 5px auto;
}
.flag-switch:before, .flag-switch:after {
position: absolute;
color: white;
font-family: 'Raleway', sans-serif;
top: 8px;
font-size: 13px;
font-weight: 700;
}
.flag-switch:before {
left: -19px;
content: attr(data-first-lang);
}
.flag-switch:after {
right: -17px;
content: attr(data-second-lang);
}
.flag-switch input {
display: none;
}
.flag-switch input + label {
display: block;
position: absolute;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
width: 60px;
}
.flag-switch input + label:before, .flag-switch input + label:after {
content: "";
position: absolute;
border-radius: 30px;
-webkit-transition: all 0.25s ease-in-out;
transition: all 0.25s ease-in-out;
}
.flag-switch input + label:before {
height: 30px;
width: 60px;
background-color:#EE4B53 ;
}
.flag-switch input + label:after {
top: 3px;
left: 3px;
border: 2px solid #DFDFDF;
width: 24px;
height: 24px;
background: #eb3 url(http://i.imgur.com/mYEJa6E.png) -26px center;
transform: translate(30px, 0);
-webkit-transform: translate(30px, 0);
}
.flag-switch input:checked + label:after {
background: #000233 url(http://i.imgur.com/mYEJa6E.png) -2px center;
-webkit-transition: all 0.25s ease-in-out;
transition: all 0.25s ease-in-out;
-webkit-transform: translate(0, 0);
transform: translate(0, 0);
}
.flag-switch input:checked + label:before {
background-color: #1FA9D6;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<div class="flag-switch" data-first-lang="GR" data-second-lang="EN">
<input type="checkbox" id="check1" >
<label for="check1"></label>
</div>
相关文章:
- 不正确的复选框重定向
- 高级表单提交-单选、复选框和重定向
- 单击复选框时重定向
- 如何在提交时防止粘性复选框重置?(一页上有多个表格)
- HTML5/Javascript验证复选框提交URL重定向
- 一次选中4个复选框,然后将页面重定向到新页面
- 重定向到不同的页面,基于选中的复选框组合
- 将复选框重置为基于先前复选框的页面加载状态's状态
- 使用 jQuery 重定向复选框
- j查询单击事件重置复选框的检查属性
- 动态复选框被重置
- 从更新下拉框重置复选框
- Jstree:当我重新加载树时,我不能将所有的复选框重置为默认取消选中
- 根据从弹出窗口中选择的复选框,重定向
- 重定向取决于选中哪个复选框
- JQGRID-保持复选框选择状态-页面刷新/重定向/重新加载
- 复选框选择时的表单重定向
- 无法在angular js中将复选框重置为未选中
- Ajax表单在提交数据时忽略我的复选框重置为默认值
- 当点击一行时重定向到页面,当点击复选框时启用按钮.'