Jquery 获取元素的高度并在css中使用它
我的页面上有一个Jquery 获取元素的高度并在css中使用它,jquery,outerheight,Jquery,Outerheight,我的页面上有一个div元素,其中包含一个p元素 <div class="parent-div"> <p>Here's some text</p> </div> 这里有一些文字 我试图获得p元素的外部高度,然后使用该数字将边距应用于父元素 e、 g如果p元素的高度是346px,那么我想对父div应用一个346px的边距顶部 我可以使用jQuery的outerHeight()获得高度,但我不知道如何使用该数字将边距应用于父元素 任何建
div
元素,其中包含一个p
元素
<div class="parent-div">
<p>Here's some text</p>
</div>
这里有一些文字
我试图获得p
元素的外部高度,然后使用该数字将边距应用于父元素
e、 g如果p
元素的高度是346px,那么我想对父div
应用一个346px的边距顶部
我可以使用jQuery的outerHeight()
获得高度,但我不知道如何使用该数字将边距应用于父元素
任何建议都将不胜感激
var height = $("#yourelement").outerHeight();
$("#parent").css('margin-top', height);
您可以使用“css”方法将样式应用于元素
$('p').each(function(){//for each paragraph
$(this).parent().css("margin-top", $(this).outerHeight() +"px");
//change its parent margin-top with the current p's outerHeight
})
您可以使用“css”方法将样式应用于元素
$('p').each(function(){//for each paragraph
$(this).parent().css("margin-top", $(this).outerHeight() +"px");
//change its parent margin-top with the current p's outerHeight
})
您可以使用“css”方法将样式应用于元素
$('p').each(function(){//for each paragraph
$(this).parent().css("margin-top", $(this).outerHeight() +"px");
//change its parent margin-top with the current p's outerHeight
})
您可以使用“css”方法将样式应用于元素
$('p').each(function(){//for each paragraph
$(this).parent().css("margin-top", $(this).outerHeight() +"px");
//change its parent margin-top with the current p's outerHeight
})