我必须如何更改我的代码
How do I have to change my codes?
每当我想单击标签时,我的子级元素都会显示没关系,它就像我想要的那样好,但是如果我单击复选框(不是标签),则应选中我的复选框或通过其他方式必须为填充复选标记(如果我单击标签,我的复选框必须是空检查)。
这篇文章解释了我上一篇文章的解释,我想这是不可能的,我必须改变我的结构才能做我想做的事
代码笔演示
我的网页
<html lang="en">
<head>
<meta charset="UTF-8">
<title>No Title</title>
</head>
<body>
<div class="my-checkbox">
</div>
<div class="new-checkbox">
<ul>
<li class="hasUl">
<input type="checkbox" id="input1">
<label for="input1">kategori <strong>(1)</strong>
</label>
<ul>
<li>
<input type="checkbox" id="input11">
<label for="input11">kategori<strong>(11)</strong>
</label>
</li>
<li>
<input type="checkbox" id="input12">
<label for="input12">kategori <strong>(12)</strong>
</label>
</li>
<li>
<input type="checkbox" id="input13">
<label for="input13">kategori <strong>(13)</strong>
</label>
</li>
</ul>
</li>
<li>
<input type="checkbox" id="input2">
<label for="input2">kategori <strong>(2)</strong>
</label>
</li>
<li class="hasUl">
<input type="checkbox" id="input3">
<label for="input3">kategori <strong>(3)</strong>
</label>
<ul>
<li>
<input type="checkbox" id="input31">
<label for="input31">kategori <strong>(31)</strong>
</label>
</li>
<li>
<input type="checkbox" id="input32">
<label for="input32">kategori <strong>(32)</strong>
</label>
</li>
<li>
<input type="checkbox" id="input33">
<label for="input33">kategori <strong>(33)</strong>
</label>
<ul>
<li>
<input type="checkbox" id="input331">
<label for="input331">kategori <strong>(331)</strong>
</label>
</li>
<li>
<input type="checkbox" id="input332">
<label for="input332">kategori <strong>(332)</strong>
</label>
</li>
<li>
<input type="checkbox" id="input333">
<label for="input333">kategori <strong>(333)</strong>
</label>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div><!-- new checkbox-->
<script type="text/javascript" src="https://cdn.anitur.com.tr/js/jquery-1.8.2.min.js" ></script>
</body>
</html>
我的 CSS
.new-checkbox ul {
padding: 0;
margin: 0;
list-style: none;
margin-left: 30px;
font: normal 11px/16px"Segoe UI", Arial, Sans-serif;
}
.new-checkbox ul:first-child {
margin-left: 0;
}
.new-checkbox ul li {
margin: 3px 0;
}
.new-checkbox input[type="checkbox"] {
display: none;
}
.new-checkbox label {
cursor: pointer;
}
.new-checkbox input[type="checkbox"] + label:before {
border: 1px solid #ffffff;
content: "'00a0";
display: inline-block;
font: 16px/1em sans-serif;
height: 13px;
width: 13px;
margin: 2px .25em 0 0;
padding: 0;
vertical-align: top;
border: solid 1px #1375b3;
color: #1375b3;
opacity: .50;
}
.new-checkbox input[type="checkbox"]:checked + label:before {
background: #fff;
color: #1375b3;
content: "'2714";
text-align: center;
box-shadow: 0 0 2px rgba(0, 0, 0, .25) inset;
opacity: 1;
}
.additional:before{
border: 1px solid #ffffff;
content: "'00a0" !important;
display: inline-block;
font: 16px/1em sans-serif;
height: 13px;
width: 13px;
margin: 2px .25em 0 0;
padding: 0;
vertical-align: top;
border: solid 1px #1375b3;
color: #1375b3;
opacity: .50;
}
.new-checkbox input[type="checkbox"]:checked + label:after {
font-weight: bold;
}
.new-checkbox ul li:before {
content: "'25b6";
display: inline-block;
margin: 2px 0 0;
width: 13px;
height: 13px;
vertical-align: top;
text-align: center;
color: #e74c3c;
font-size: 8px;
line-height: 13px;
cursor: pointer;
}
li.parent.has-checked:before {
content: "'25bc" !important;
display: inline-block;
margin: 2px 0 0;
width: 13px;
height: 13px;
vertical-align: top;
text-align: center;
color: #e74c3c;
font-size: 8px;
line-height: 13px;
cursor: pointer;
}
.new-checkbox li {
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
}
.new-checkbox input[type="checkbox"][id]:checked ~ li::before {
content: "'25bc";
}
.new-checkbox li ul {
display: none;
}
.new-checkbox li.has-checked > ul {
display: block;
}
和我的j查询
$(document).ready(function() {
$('.new-checkbox li:has(ul)').addClass('parent');
$(".new-checkbox li.hasUl > label:not(:input[type='checkbox'])").on("click",function(){
$(this).addClass("additional");
});
$('.new-checkbox input[type=checkbox]:not(label)').on("change", function() {
var checked = this.checked,
$li = $(this).parent();
$li.find('input[type=checkbox]').prop('checked', checked).parent().toggleClass('has-checked', checked);
$li.parentsUntil('.new-checkbox', 'li').each(function() {
var $checks = $(this).find('ul input[type=checkbox]');
$(this).children('input[type=checkbox]').prop('checked', !$checks.filter(':not(:checked)').length);
$(this).toggleClass('has-checked', $checks.is(':checked'));
});
});
});
不需要 JavaScript:
.check-toggle input[type=checkbox] + label + div{
display:none;
}
.check-toggle input[type=checkbox]:checked + label + div{
display:block;
}
<div class="check-toggle">
<input type="checkbox" id="my-id">
<label for="my-id">Check me</label>
<div>
Some content here
</div>
</div>
相关文章:
- 元素在我的代码中不会.fadeTo.Don'I don’我不知道;s错误的JavaScript、JQuery、H
- 验证我的代码json
- 如何创建更好的方式来维护基于我的代码访问的最后一个页面
- 我不知道为什么我的代码是错误的?又有什么错
- 我需要一个jQuery函数来只工作在700px以上的屏幕大小,无法在我的代码中发现错误
- I'我不知道为什么我的代码没有'不起作用
- 为什么动态加载的事件在我的代码中不能正常工作
- 如何禁用我的切换按钮?这是我的代码
- 我需要将条形图集成到我的代码中
- onbeforeunload事件在我的代码中没有触发,但其他示例可以工作
- 我的代码在<脚本>标记,但没有'不能在外部文件中工作
- 在我的代码中,实体类"卡片展示”;不起作用
- Webrtc和socket.io:createanswer()总是有一个错误.为什么?我的代码连接了两个对等体,但其中只
- 我的代码很完美,但我现在不知道为什么我发现了问题
- Html、css和jQuery.我的代码有问题
- 鼠标事件,我的代码出了什么问题
- 我怎样才能修复我的代码,以便<罢工>不会使我的 Javascript 代码无效
- 如何在我的“代码块”中使表格 Squarespace 网站响应式
- 如何使我的代码更快
- 在我的代码中管理大量硬编码数据的最佳方法