微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用jQuery获取段落高度?

如何解决如何使用jQuery获取段落高度?

| 我一直在尝试使用$(\ .. 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>&nbsp;</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>
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。