gpt4 book ai didi

jquery - 如果子项具有 CSS 并使用 jQuery 获取子项索引,则调整父项的大小

转载 作者:行者123 更新时间:2023-11-28 13:31:58 24 4
gpt4 key购买 nike

如果 child 高度发生变化,我正在尝试动态更改多个 parent 的高度。

每个 parent 只有一个 z-index 样式的 child ..

这是我根据我的发现尝试做的

HTML

<div class="parent">
<div class="child"></div>
<div class="child2"></div>
<div class="child3"></div>
</div>
<br>

<div class="parent">
<div class="child" style="z-index:1;"></div>
<div class="child2"></div>

</div>
<br>

<div class="parent">
<div class="child" ></div>
<div class="child2" style="z-index:1;"></div>

</div>

JQUERY

$(".parent").each(function() {

var divIndex = ;
var divHeight = ;

if ($('this').children().css('z-index') == '1')
{
// Get height and index of the single specific element with z-index css
}

$('this').resize();
$('this').delegate();


});

JSFiddle

最佳答案

您的代码充满了 javascript 错误,更具体地说是 $('this') 的使用,去掉单引号:$(this);

由于使用 id 而不是类,您的大部分 CSS 都不会设置样式。

这是一个更新的 jsfiddle,它更正了错误并会让您走上迭代父子 div 的正确轨道:

http://jsfiddle.net/EgbLk/4/

$('.parent').children().each(
function(){

var divIndex = null ;
var divHeight = null ;

if($(this).css('z-index') === '1') {
alert('z-index is 1');
}

$(this).parent().resize();
$(this).parent().delegate();
}
);

这里是一些“更正”的 CSS:

.parent {
width: 100px;
padding: 10px;
background:#ff0000;
}

.child2 {
position: absolute;
width: auto;
left: 0px;
right: 0px;
height:20px
}

.child {
position: absolute;
width: auto;
left: 0px;
right: 0px;
height:50px
z-index:1;
}

.child3 {
position: absolute;
width: auto;
left: 0px;
right: 0px;
height:100px
}

关于jquery - 如果子项具有 CSS 并使用 jQuery 获取子项索引,则调整父项的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11350382/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com