javascript - 循环遍历 div 以定义父级高度
问题描述
有多个部分。每个部分都有多个选项卡。我想获取第一个选项卡高度(.tab-content)并将其高度应用于它所在的部分(.tab)。这需要单独发生在每个部分 - 因此是循环。该循环目前似乎没有参与,并且 .tab-content 上的第一个高度正在应用于所有部分。为什么我的循环不起作用?
现场示例:https ://staging.123innovation.co.uk/careers/faq
JS
function tabParentHeight() {
$(".tabs").each(function() {
var ph = $(this).outerHeight();
$(this).find('.tab-content:first').css('min-height', 0);
var ch = $(this).find('.tab-content:first').outerHeight();
if (ch > ph) {
$(this).css({
'height': ch + 'px'
});
}
});
}
$(document).ready(function () {
// TABS
// Run Tab Height
tabParentHeight();
$('section a').click(function(event) {
event.preventDefault();
$(this).addClass('active');
$(this).siblings().removeClass('active');
var ph = $(this).parent().height();
var ch = $(this).next().height();
if (ch > ph) {
$(this).parent().css({
'height': ch + 'px'
});
} else {
$(this).parent().css({
'height': 'auto'
});
}
});
});
$(window).resize(function() {
// Run Tab Height
tabParentHeight();
});
$(document).resize(function() {
// Run Tab Height
tabParentHeight();
});
HTML
<section class="tab">
<a class="tab-link link pointer blue hover-teal no-underline d tab-link w-33-ns pb3 ba bl-0 bt-0 br-0 b--dotted b--black-30 db relative active" target="_blank">
<span class="fa-li"><i class="fas fa-caret-down blue teal rm-90"></i></span>
Title One
</a>
<div id="tab-21" class="tab-content w-two-thirds-ns h-auto pt3 pb2 pl3 pl5-ns">
<h3 class="dn db-ns">Content One</h3>
<p>Content goes here</p>
</div>
<a class="tab-link link pointer blue hover-teal no-underline d tab-link w-33-ns pb3 ba bl-0 bt-0 br-0 b--dotted b--black-30 db relative" target="_blank">
<span class="fa-li"><i class="fas fa-caret-down blue teal rm-90"></i></span>
Title One
</a>
<div id="tab-21" class="tab-content w-two-thirds-ns h-auto pt3 pb2 pl3 pl5-ns">
<h3 class="dn db-ns">Content Two</h3>
<p>Content goes here</p>
</div>
<a class="tab-link link pointer blue hover-teal no-underline d tab-link w-33-ns pb3 ba bl-0 bt-0 br-0 b--dotted b--black-30 db relative" target="_blank">
<span class="fa-li"><i class="fas fa-caret-down blue teal rm-90"></i></span>
Title One
</a>
<div id="tab-21" class="tab-content w-two-thirds-ns h-auto pt3 pb2 pl3 pl5-ns">
<h3 class="dn db-ns">Content Three</h3>
<p>Content goes here</p>
</div>
</section>
<section class="tab list ml2 pl0 tf w-100 h-auto db relative">
<a class="tab-link link pointer blue hover-teal no-underline d tab-link w-33-ns pb3 ba bl-0 bt-0 br-0 b--dotted b--black-30 db relative active" target="_blank">
<span class="fa-li"><i class="fas fa-caret-down blue teal rm-90"></i></span>
Title One
</a>
<div id="tab-21" class="tab-content w-two-thirds-ns h-auto pt3 pb2 pl3 pl5-ns">
<h3 class="dn db-ns">Content One</h3>
<p>Content goes here</p>
</div>
<a class="tab-link link pointer blue hover-teal no-underline d tab-link w-33-ns pb3 ba bl-0 bt-0 br-0 b--dotted b--black-30 db relative" target="_blank">
<span class="fa-li"><i class="fas fa-caret-down blue teal rm-90"></i></span>
Title One
</a>
<div id="tab-21" class="tab-content w-two-thirds-ns h-auto pt3 pb2 pl3 pl5-ns">
<h3 class="dn db-ns">Content Two</h3>
<p>Content goes here</p>
</div>
<a class="tab-link link pointer blue hover-teal no-underline d tab-link w-33-ns pb3 ba bl-0 bt-0 br-0 b--dotted b--black-30 db relative" target="_blank">
<span class="fa-li"><i class="fas fa-caret-down blue teal rm-90"></i></span>
Title One
</a>
<div id="tab-21" class="tab-content w-two-thirds-ns h-auto pt3 pb2 pl3 pl5-ns">
<h3 class="dn db-ns">Content Three</h3>
<p>Content goes here</p>
</div>
</section>
解决方案
不确定这些元素是如何生成的,但由于某种原因,它导致了问题。所有选项卡内容都具有min-height: 100%
属性,并且由于您添加了脚本,因此所有选项卡的高度都与第一个相同。
无论如何要解决此问题,您可以删除 css 中的min-height: 100%
intab-content
类或在脚本中执行以下操作:
$('.tabs').each(function() {
$(this).find('.tab-content').css('min-height', 0);
$(this).outerHeight($(this).find('.tab-content').outerHeight());
});
编辑:您可以将 tabParentHeight() 函数替换为:
$(".tabs").each(function() {
var ph = $(this).outerHeight();
$(this).find('.tab-content:first').css('min-height', 0);
var ch = $(this).find('.tab-content:first').outerHeight();
if (ch > ph) {
$(this).css({
'height': ch + 'px'
});
}
});
推荐阅读
- node.js - 保持 sitemap.xml 更新的最佳方法是什么
- android - 自定义视图有奇怪的布局行为
- sql - Oracle SQL 查询让所有代理直接或间接向经理报告
- php - 我在尝试使用 testnet 在 block.io 上传输时遇到错误
- c++ - 一组自定义类对象及其 < 运算符
- java - Weblogic 12c 上的 Spring-boot 之战
- php - 使用键名将多个对象值添加到数组
- excel - 选择两个单词之间的范围,然后根据不同的列删除它们
- node.js - 执行节点时出错(关于 firebase),但应用程序运行良好,即使使用 firebase(角度通用)
- javascript - 如果 Cookie 存在,则 JS 重定向