- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
如果子 sub-list
没有事件
类。如果 sub-list
属于选定的主要类别(单击 Main1 或 Main 2),则它接收 active
类)
我尝试了这段代码但没有成功:
var item = $(".sub-list");
if (!$(item).hasClass("active")) {
$(item)
.closest(".sub-item")
.hide();
} else {
$(item)
.closest(".sub-item")
.show();
}
在代码片段中,sub-items
不会hide();
即使 sub-list
接收到 活跃的
类。如何隐藏所有具有 active
类的子项
?
$(document).ready(function() {
function toggleSubcatDiv(input, showTypeList) {
var $list = $(input).closest(".post-item");
$list
.find(".sub-list")
.filter(".active")
.removeClass("active")
.end()
.filter('[data-for="' + $(input).attr("name") + '"]')
.addClass("active");
}
$(this).on("click click.init", ".cat-item", function(e) {
var $menu = $(this).closest(".cat-dropdown");
var item = $(".sub-list");
if (!$(item).hasClass("active")) {
$(item)
.closest(".sub-item")
.hide();
} else {
$(item)
.closest(".sub-item")
.show();
}
$menu
.find(".title")
.children(":checkbox")
.prop("checked", true)
.next("span")
.text($(this).text())
.css("color", this.style.color);
toggleSubcatDiv($(":radio", this), "init" !== e.namespace);
});
});
.sub-item {
min-height: 10px
}
.sub-item input {
display: none
}
.cat1 {
border: 2px solid blue;
}
.cat2 {
border: 2px solid red;
}
.sub-list:not(.active) {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="post-item">
Main Category:
<div class="cat-dropdown closed">
<div class="edit">
<label class="title">
<input type="checkbox"><span> Choose...</span>
</label>
<div class="cat-dropdown-menu">
<div class="cat-list" id="category">
<label class="cat-item" style="color:blue">
<input type="radio" name="cat1">Main 1</label>
<label class="cat-item" style="color:red">
<input type="radio" name="cat2">Main 2</label>
</div>
</div>
</div>
</div>
Sub Category:
<div class="sub-container">
<div class="type-list">
<div class="sub-item cat1">
<div class="sub-list" data-for="cat1">
<label class="sub-btn" for="comp">
<input class="sub-input" id="comp" type="radio" name="radios" />
<span class="sub-icon">
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="25"><path id="patha" fill="blue" d="M21.72 16.784a.094.094 0 0 0-.071-.033H3.351a.094.094 0 0 0-.093.108c.003.017.279 1.711 2.421 1.711h13.642c2.142 0 2.418-1.694 2.42-1.711a.095.095 0 0 0-.021-.075zm-7.844 1.193h-2.752v-.633h2.752v.633zM5.252 16.092h14.496c.21 0 .38-.17.38-.38V6.81a.38.38 0 0 0-.38-.38H5.252a.38.38 0 0 0-.38.38v8.904c0 .21.17.38.38.38zM6.2 7.758h12.598v7.006H6.201V7.758z"/></svg><br><b>Main 1A</b></span></label>
</div>
</div>
<div class="sub-item cat1">
<div class="sub-list" data-for="cat1">
<label class="sub-btn" for="ticket">
<input class="sub-input" id="ticket" type="radio" name="radios" />
<span class="sub-icon">
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="25"><path id="patha2" fill="blue" d="M7.519 14.008l3.473 3.473 6.372-6.372-3.473-3.473-6.372 6.372zm13.884-4.77L19.925 7.76a1.98 1.98 0 0 1-2.686-2.686l-1.477-1.477a.992.992 0 0 0-1.4 0L3.597 14.362a.992.992 0 0 0 0 1.4l1.478 1.478a1.98 1.98 0 0 1 2.685 2.686l1.478 1.477a.993.993 0 0 0 1.4 0l10.765-10.765a.993.993 0 0 0 0-1.4zM10.992 19.07L5.93 14.008l7.962-7.96 5.06 5.061-7.96 7.96z"/></svg><br><b>Main 1B</b></span></label>
</div>
</div>
<div class="sub-item cat2">
<div class="sub-list" data-for="cat2">
<label class="sub-btn" for="elephant">
<input class="sub-input" id="elephant" type="radio" name="radios" />
<span class="sub-icon">
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="25"><path id="patha3" fill="red" d="M19.565 9.341h-4.086a4.636 4.636 0 0 1-1.67 3.027.476.476 0 0 1-.672-.067.478.478 0 0 1 .066-.674 3.684 3.684 0 0 0 1.354-2.858 3.7 3.7 0 0 0-3.695-3.695A3.7 3.7 0 0 0 7.188 8.38a3.448 3.448 0 0 0-.86.702c-.826.936-1.008 2.16-1.008 3.053 0 .382.01.765.02 1.135.03 1.11.066 2.493-.3 2.87-.029.029-.115.117-.422.117a.957.957 0 1 0 0 1.914c.74 0 1.343-.234 1.793-.696.6-.617.795-1.505.846-2.513.113.003.234.005.36.005.955 0 2.245-.115 3.266-.627 0 0-.07.442-.07.568v3.807c0 .68.643 1.211 1.325 1.211h.018c.682 0 1.23-.53 1.23-1.21v-1.96h4.544v1.975c0 .672.565 1.195 1.237 1.195h.018c.672 0 1.197-.523 1.197-1.195v-2.182c.598-.3.957-.898.957-1.586v-3.828c0-.99-.783-1.794-1.774-1.794zM7.189 12.107a.451.451 0 1 1 .902 0 .451.451 0 0 1-.902 0z"/></svg><br><b>Main 2A</b></span></label>
</div>
</div>
<div class="sub-item cat2">
<div class="sub-list" data-for="cat2">
<label class="sub-btn" for="hook">
<input class="sub-input" id="hook" type="radio" name="radios" />
<span class="sub-icon">
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="25"><path id="patha4" fill="red" d="M13.587 16.6h1.829c-.254 1.303-1.405 2.31-2.772 2.31a2.836 2.836 0 0 1-2.823-2.836v-5.166a.853.853 0 0 0 .25-.611V5.884a.779.779 0 0 0-.776-.793.779.779 0 0 0-.776.793v4.413c0 .246.1.466.25.611v5.166c0 2.115 1.74 3.835 3.854 3.835 2.09 0 3.821-1.717 3.846-3.83l.012-1.97v-1.573L13.587 16.6z"/></svg><br><b>Main 2B</b></span></label>
</div>
</div>
<div class="sub-item cat2">
<div class="sub-list" data-for="cat2">
<label class="sub-btn" for="plane">
<input class="sub-input" id="plane" type="radio" name="radios" />
<span class="sub-icon">
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="25"><path id="patha5" fill="red" d="M20.061 15.32v-.868l-6.14-4.27.14-3.993c-.071-2.093-1.417-2.427-1.614-2.427-.196 0-1.526.44-1.507 2.427l.14 3.992-6.141 4.271v.868l6.28-1.167.16 4.568-2.294 1.595v.392h2.493a1.07 1.07 0 0 0 1.846 0h2.49v-.392l-2.292-1.595.16-4.568 6.28 1.167z"/></svg><br><b>Main 2C</b></span></label>
</div>
</div>
</div>
</div>
</div>
最佳答案
问题是由 $(".sub-list")
返回多个元素引起的。
因此,当您使用 .hasClass()
进行测试时,您总是得到一个 true
,因为至少返回的一个元素具有该类。
您必须运行 .each()
循环来分别测试所有元素:
var item = $(".sub-list");
item.each(function(){
if (!$(this).hasClass("active")) {
$(this)
.closest(".sub-item")
.hide();
} else {
$(this)
.closest(".sub-item")
.show();
}
});
我还为单选按钮添加了一个快速修复...要取消选中另一个:
$(".cat-item").not(this).find("input").prop("checked",false);
您的代码段已通过上述更改修复:
$(document).ready(function() {
function toggleSubcatDiv(input, showTypeList) {
var $list = $(input).closest(".post-item");
$list
.find(".sub-list")
.filter(".active")
.removeClass("active")
.end()
.filter('[data-for="' + $(input).attr("name") + '"]')
.addClass("active");
}
$(this).on("click click.init", ".cat-item", function(e) {
$(".cat-item").not(this).find("input").prop("checked",false);
var $menu = $(this).closest(".cat-dropdown");
var item = $(".sub-list");
item.each(function(){
if (!$(this).hasClass("active")) {
$(this)
.closest(".sub-item")
.hide();
} else {
$(this)
.closest(".sub-item")
.show();
}
});
$menu
.find(".title")
.children(":checkbox")
.prop("checked", true)
.next("span")
.text($(this).text())
.css("color", this.style.color);
toggleSubcatDiv($(":radio", this), "init" !== e.namespace);
});
});
.sub-item {
min-height: 10px
}
.sub-item input {
display: none
}
.cat1 {
border: 2px solid blue;
}
.cat2 {
border: 2px solid red;
}
.sub-list:not(.active) {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="post-item">
Main Category:
<div class="cat-dropdown closed">
<div class="edit">
<label class="title">
<input type="checkbox"><span> Choose...</span>
</label>
<div class="cat-dropdown-menu">
<div class="cat-list" id="category">
<label class="cat-item" style="color:blue">
<input type="radio" name="cat1">Main 1</label>
<label class="cat-item" style="color:red">
<input type="radio" name="cat2">Main 2</label>
</div>
</div>
</div>
</div>
Sub Category:
<div class="sub-container">
<div class="type-list">
<div class="sub-item cat1">
<div class="sub-list" data-for="cat1">
<label class="sub-btn" for="comp">
<input class="sub-input" id="comp" type="radio" name="radios" />
<span class="sub-icon">
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="25"><path id="patha" fill="blue" d="M21.72 16.784a.094.094 0 0 0-.071-.033H3.351a.094.094 0 0 0-.093.108c.003.017.279 1.711 2.421 1.711h13.642c2.142 0 2.418-1.694 2.42-1.711a.095.095 0 0 0-.021-.075zm-7.844 1.193h-2.752v-.633h2.752v.633zM5.252 16.092h14.496c.21 0 .38-.17.38-.38V6.81a.38.38 0 0 0-.38-.38H5.252a.38.38 0 0 0-.38.38v8.904c0 .21.17.38.38.38zM6.2 7.758h12.598v7.006H6.201V7.758z"/></svg><br><b>Main 1A</b></span></label>
</div>
</div>
<div class="sub-item cat1">
<div class="sub-list" data-for="cat1">
<label class="sub-btn" for="ticket">
<input class="sub-input" id="ticket" type="radio" name="radios" />
<span class="sub-icon">
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="25"><path id="patha2" fill="blue" d="M7.519 14.008l3.473 3.473 6.372-6.372-3.473-3.473-6.372 6.372zm13.884-4.77L19.925 7.76a1.98 1.98 0 0 1-2.686-2.686l-1.477-1.477a.992.992 0 0 0-1.4 0L3.597 14.362a.992.992 0 0 0 0 1.4l1.478 1.478a1.98 1.98 0 0 1 2.685 2.686l1.478 1.477a.993.993 0 0 0 1.4 0l10.765-10.765a.993.993 0 0 0 0-1.4zM10.992 19.07L5.93 14.008l7.962-7.96 5.06 5.061-7.96 7.96z"/></svg><br><b>Main 1B</b></span></label>
</div>
</div>
<div class="sub-item cat2">
<div class="sub-list" data-for="cat2">
<label class="sub-btn" for="elephant">
<input class="sub-input" id="elephant" type="radio" name="radios" />
<span class="sub-icon">
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="25"><path id="patha3" fill="red" d="M19.565 9.341h-4.086a4.636 4.636 0 0 1-1.67 3.027.476.476 0 0 1-.672-.067.478.478 0 0 1 .066-.674 3.684 3.684 0 0 0 1.354-2.858 3.7 3.7 0 0 0-3.695-3.695A3.7 3.7 0 0 0 7.188 8.38a3.448 3.448 0 0 0-.86.702c-.826.936-1.008 2.16-1.008 3.053 0 .382.01.765.02 1.135.03 1.11.066 2.493-.3 2.87-.029.029-.115.117-.422.117a.957.957 0 1 0 0 1.914c.74 0 1.343-.234 1.793-.696.6-.617.795-1.505.846-2.513.113.003.234.005.36.005.955 0 2.245-.115 3.266-.627 0 0-.07.442-.07.568v3.807c0 .68.643 1.211 1.325 1.211h.018c.682 0 1.23-.53 1.23-1.21v-1.96h4.544v1.975c0 .672.565 1.195 1.237 1.195h.018c.672 0 1.197-.523 1.197-1.195v-2.182c.598-.3.957-.898.957-1.586v-3.828c0-.99-.783-1.794-1.774-1.794zM7.189 12.107a.451.451 0 1 1 .902 0 .451.451 0 0 1-.902 0z"/></svg><br><b>Main 2A</b></span></label>
</div>
</div>
<div class="sub-item cat2">
<div class="sub-list" data-for="cat2">
<label class="sub-btn" for="hook">
<input class="sub-input" id="hook" type="radio" name="radios" />
<span class="sub-icon">
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="25"><path id="patha4" fill="red" d="M13.587 16.6h1.829c-.254 1.303-1.405 2.31-2.772 2.31a2.836 2.836 0 0 1-2.823-2.836v-5.166a.853.853 0 0 0 .25-.611V5.884a.779.779 0 0 0-.776-.793.779.779 0 0 0-.776.793v4.413c0 .246.1.466.25.611v5.166c0 2.115 1.74 3.835 3.854 3.835 2.09 0 3.821-1.717 3.846-3.83l.012-1.97v-1.573L13.587 16.6z"/></svg><br><b>Main 2B</b></span></label>
</div>
</div>
<div class="sub-item cat2">
<div class="sub-list" data-for="cat2">
<label class="sub-btn" for="plane">
<input class="sub-input" id="plane" type="radio" name="radios" />
<span class="sub-icon">
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="25"><path id="patha5" fill="red" d="M20.061 15.32v-.868l-6.14-4.27.14-3.993c-.071-2.093-1.417-2.427-1.614-2.427-.196 0-1.526.44-1.507 2.427l.14 3.992-6.141 4.271v.868l6.28-1.167.16 4.568-2.294 1.595v.392h2.493a1.07 1.07 0 0 0 1.846 0h2.49v-.392l-2.292-1.595.16-4.568 6.28 1.167z"/></svg><br><b>Main 2C</b></span></label>
</div>
</div>
</div>
</div>
</div>
关于javascript - 隐藏没有类别的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52867585/
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 9 个月前关闭。 Improve
我使用 partykit打包并遇到以下错误消息: Error in matrix(0, nrow = mi, ncol = nl) : invalid 'nrow' value (too large
我一直在尝试寻找一个量表或分类指标,为 VADER 情绪分析分配一些情感程度,而不仅仅是积极、消极或中性。如果有人可以分享他们的观点或资源来帮助按以下方式对 VADER 复合分数进行分类,我将非常感激
伙计们,我想自动循环..但我不知道是我放错了 while 还是循环错了? 我的数据库 标签:kt_barang kd_kategori | nama_kategori 1
我正在创建一个列出本地企业并按类别、子类别和关键字对它们进行分组的应用程序。以下是企业排序规则: 一个企业可以属于多个类别和子类别 一个企业可以有多个关键字 并非每个类别都有子类别,但有子类别的只有两
我有一系列单词 - 我的刺激 - 它们显示在屏幕上。然而,每个词都有另一个“条件”,即它们是类别 A、类别 B 或类别 C。这可能很简单,但我找不到答案并坚持下去。我的最终目标是在每次运行脚本时将类别
我正在使用 Laravel 5.5 和 MySql。如果不向 Services 表中添加 subCategoryID 列,我无法弄清楚如何将类别和子类别与服务相关联。 目前这是我的表结构 服务类别 i
我有两个部分/类别结构的链接表。 the section table structure id sec_title 1 section 1 2 section 2 the category str
我有一个类层次结构如下 @interface PTLDatasource : NSObject ... @interface PTLFetchedDatasource : PTLDatasource
我有一个 DataFrame df 一列,category 使用以下代码创建: import pandas as pd import random as rand from string import
我经常在多个类中设置获取请求以从核心数据(加上一些其他结果)中检索“allRecipes”或“lastModifiedDate”。 为此使用专门的类别 NSManagedObjectContext+R
可以在 Objective C 中创建类别之间的依赖关系吗?也在类别和它们的基类之间? 我知道在运行时应该没有区别,它们可能只是在编译时合并在一起。例如,假设我将 B 类分解为: B(base cla
这个问题在这里已经有了答案: 关闭 10 年前。
example img of a category selection by user 嘿,我正在尝试设置一个选择,用户必须选择一个类别和第二个类别,但我不知道如何获取他单击的信息。用户单击类别后,它
尝试将投资组合库添加到我正在制作的自定义 wp 主题中。我已经筋疲力尽地试图寻找甚至可以修改一些的解决方案和插件。我认为我在寻找解决方案时遇到的一个问题是我不完全确定哪些搜索词可以帮助我找到与我想要实
当我查看 Cocoa Touch API 时,我可以在同一个头文件中找到一些与类别一起声明的类,例如 @interface NSArray : NSObject @property (readonl
我的 log4j.properties 中有以下内容 log4j.rootLogger = debug, stdout, fileLog log4j.appender.stdout = org.apa
如果我在类中添加类别方法,比如NSXMLNode: @interface NSXMLNode (mycat) - (void)myFunc; @end NSXMLNode 的子类,例如 NSXMLEl
先说场景,wordpress的分类结构是这样的 Level 1: Top Level 2: -Nextme_1 Level 3: --Nextme_2 --Nextme_3 Leve
我有一个解析网络,现在我想浏览标签,或显示图表。我怎样才能得到图表?或者在树中导航。显示第一步然后其他等。并了解这棵树是如何 build 的。 import urllib from lxml impo
我是一名优秀的程序员,十分优秀!