【发布时间】:2018-08-10 21:16:20
【问题描述】:
我想在下面的示例中获取类 .child 的高度值,然后使用警报显示该值以进行故障排除。
我有两个元素 .first 和 .second 是兄弟姐妹,.second 元素有一个名为 .child 的子元素
HTML:
<div class="parent">
<div class="first">Click Me</div>
<div class="second">
<div class="child"></div>
</div>
</div>
CSS:
.first {
width: 60px;
padding: 8px;
background: blue;
}
.child {
height: 1234px;
}
jQuery:
$(".first").click(function() {
var childHeight = $(this).next(".second > .child").outerHeight();
alert(childHeight);
});
问题似乎在于针对孩子,如果我从我的 var 中删除 > .child,它会返回 .second 的高度
这是一个 JS fiddle,代码相同:https://jsfiddle.net/CultureInspired/6dxLp86b/
【问题讨论】:
-
我也试过用 find 代替 next,以及 nextAll 或 nextUntil
标签: javascript jquery parent-child next siblings