如何正确检查第一子节点是否隐藏并设置函数Bool
How to Properly Check if First Child is Hidden and Then Set Function Bool
我正试图回答我最近的一个问题,我相信最好的答案涉及到一个函数偷看第一个子元素,然后设置一个bool在该函数之外的另一个函数使用。
回顾一下:我有一个GridView它的结构是这样的:
-
组合
-
dept-header
- (某人实体)
- (某人实体)
- (某人实体)
-
dept-header
- (某人实体)
- (某人实体)
- (某人实体)
-
-
组合
-
dept-header
- (某人实体)
- (某人实体)
- (某人实体)
-
dept-header
- (某人实体)
- (某人实体)
- (某人实体)
-
基本上,我希望当group-header被单击时,它隐藏所有下面的行,直到下一个group-header,如果它们不隐藏,否则显示它们。
我有代码和HTML设置在https://jsfiddle.net/dLp47mtd/1/,但问题的代码是不扩展/折叠是:
// Hide/Show Group's Data on Click
$(function() {
$('.group-header').click(function() {
// Assume first dept-header not visible
var nextVisible = 0;
// Check if the first dept-header is visible
$(this).firstChild(function() {
var el = this;
// If visible, change our assumption
if (el.style.display === '') {
nextVisible = 1;
}
});
// If visible, hide all sub-elements else show all sub-elements
$(this).nextUntil('.group-header').each(function() {
var el = this;
if (nextVisible === 1) {
el.style.display = 'none';
} else {
el.style.display = '';
}
});
});
});
如果您想显示和隐藏组下的所有行,而不需要记住已经切换了哪些部门,那么这对我来说很有效。
https://jsfiddle.net/dLp47mtd/5/// Hide/Show Dept's Data on Click
$(function () {
$('.dept-header').click(function () {
$(this).nextUntil('.dept-header, .group-header').toggle();
});
});
$(function () {
$('.group-header').click(function () {
var elm = $(this);
if (elm.next().is(":visible")) {
elm.nextUntil('.group-header').hide();
} else {
elm.nextUntil('.group-header').show();
}
});
});
Demo
$('.group-header').click(function()
{
$(this).nextAll().each(function()
{
if($(this).hasClass("group-header"))
return false;
if($(this).is(":visible"))
$(this).hide();
else
$(this).show();
});
});
相关文章:
- 在jquery中为显示/隐藏设置cookie
- JavaScript 检查隐藏字段中的值并将其设置为隐藏字段值
- 如何使用javascript将值引导下拉列表设置为隐藏值asp.net
- 通过ng-if设置隐藏文本框的值
- 在提交时设置表单隐藏值
- 如何使用 JavaScriptJQuery 将隐藏输入标签的值与选择标签的选定选项的值进行设置
- 获取下一个或上一个隐藏元素的高度,以设置父元素的高度.jquery.
- 如何在代码镜像编辑器中设置隐藏值
- <asp.隐藏字段>即使在javascript中设置了值,codeehind中的值也会变为空
- getElementById().value未设置隐藏输入
- 仅当onclick和隐藏消息时设置cookie
- 如何在隐藏的文本框上设置焦点
- 根据两个下拉菜单的选择设置隐藏值
- jQuery-是否可以使用jQuery在隐藏的文本框中设置值
- 设置隐藏值后重新加载页面
- 在代码隐藏中从DataTable设置javascript数组
- Wistia Javascript API-设置视频泡沫真实和隐藏社交按钮
- 如何在使用servlet处理时使用javascript设置隐藏变量的值
- 从 C# 代码隐藏设置传单缩放区域
- 从JavaScript或代码隐藏设置AjaxFileUpload控件的te值