我一直在尝试使用$(“。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中使用display:block
,甚至height:auto
,看看我是否可以强制CSS将其视为文本块,但也没有结果。
谢谢!
答案 0 :(得分:2)
您可能正在对$(document).ready()进行声明,此时元素不会在页面上呈现,因此没有高度。你必须在$(window).load()上执行它,此时段元素将被渲染并具有高度。
答案 1 :(得分:0)
也许你应该在jquery文档中测试这个例子?例如here
这个没有任何问题:
<!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>