问题描述
|
我一直在尝试使用$(\ .. someid \“)。height()使用jQuery确定段落的高度,但是结果总是返回0。
<p class=\"someid\">Blah this is a test,I am trying to see when this wraps around what height it might have. I want to kNow how tall this paragraph element i because it can change from element to element.</p>.
我尝试在CSS中使用ѭ1,甚至使用ѭ2来查看是否可以强制CSS将其视为文本块,但那里也没有结果。
谢谢!
解决方法
您可能正在$(document).ready()上进行声明,这时该元素未在页面上呈现,因此没有高度。您必须在$(window).load()上执行此操作,此时将呈现段落元素并将其具有高度。
,也许您应该在jquery文档中测试该示例?例如这里
这一工作原理没有任何问题:
<!DOCTYPE html>
<html>
<head>
<style>
body { background:yellow; }
button { font-size:12px; margin:2px; }
p { width:150px; border:1px red solid; }
div { color:red; font-weight:bold; }
</style>
<script src=\"http://code.jquery.com/jquery-latest.js\"></script>
</head>
<body>
<button id=\"getp\">Get Paragraph Height</button>
<button id=\"getd\">Get Document Height</button>
<button id=\"getw\">Get Window Height</button>
<div> </div>
<p>
Sample paragraph to test height
</p>
<script>
function showHeight(ele,h) {
$(\"div\").text(\"The height for the \" + ele +
\" is \" + h + \"px.\");
}
$(\"#getp\").click(function () {
showHeight(\"paragraph\",$(\"p\").height());
});
$(\"#getd\").click(function () {
showHeight(\"document\",$(document).height());
});
$(\"#getw\").click(function () {
showHeight(\"window\",$(window).height());
});
</script>
</body>
</html>