我正在制作一个页面,其中另一个页面中有 2 个 div
元素:
<div id="id" class="class-name">
<div class="child-1">
</div>
<div class="child-2">
</div>
</div>
我正在尝试这个 jQuery,但它不起作用:
$(window).load( function()
{
$('.class-name').each(function()
{
var height = $(this).$('.child-1').height();
$(this).$('.child-2').css('top', -height);
});
});
不用担心类名,我在这里使用了神秘的类名
最佳答案
使用 jQuery 的 find()
或children()
方法:
var height = $(this).find('.child-1').height();
$(this).find('.child-2').css('top', -height);
或者:
var height = $(this).children('.child-1').height();
$(this).children('.child-2').css('top', -height);
关于jQuery - 如何获取类(class) child 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26296012/