Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 获取元素的高度并在css中使用它_Jquery_Outerheight - Fatal编程技术网

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
})