未返回有序/无序列表的高度

时间:2011-10-19 19:29:27

标签: jquery

jquery是否处理有序列表高度?我收到了NULL。

JS

  var highestCol = $('ol.mostrecent').height();
  console.log(highestCol);

html

    <ol class="mostpopular">
    <li class="heading"><span class="item"></span><span class='desc'>Most Popular Topics</span></li>
    <li class="first rank1 alt1"><span class='desc'><a href='/link/portal/8202/8390/Article/46/How-do-I-view-my-To-Do-List-items' class=contentLinks >How do I view my To Do List (items)?</a></span></li>
    <li class="rank2 alt2"><span class='desc'><a href='/link/portal/8202/8390/Article/99/Do-I-have-to-complete-a-FAFSA-each-year' class=contentLinks >Do I have to complete a FAFSA each year?</a></span></li>
    <li class="rank3 alt1"><span class='desc'><a href='/link/portal/8202/8390/Article/55/Find-or-Reset-Your-Password' class=contentLinks >Find or Reset Your Password </a></span></li>
    <li class="rank4 alt2"><span class='desc'><a href='/link/portal/8202/8390/Article/219/Can-I-appeal-my-Financial-Aid-Denied-status-What-is-the-process' class=contentLinks >Can I appeal my Financial Aid Denied status?  What is the process?</a></span></li>
    <li class="rank5 alt1"><span class='desc'><a href='/link/portal/8202/8390/Article/104/Grants-Available-at-NOVA' class=contentLinks >Grants Available at NOVA!</a></span></li>
  </ol>
  <ol class="mostrecent">
   <li class="heading"><span class="desc">Most Recent Topics</span></li>
    <li class="first alt1"><span class='item'>9/15/2011</span><span class='desc'><a href="/link/portal/8202/8390/Article/232/What-does-NOVA-consider-a-full-time-or-a-part-time-student" class=contentLinks >What does NOVA consider a full-time or a part-time student?</a></span></li>
    <li class="alt2"><span class='item'>9/15/2011</span><span class='desc'><a href="/link/portal/8202/8390/Article/228/Information-regarding-Veterans-Benefits" class=contentLinks >Information regarding Veterans Benefits</a></span></li>
    <li class="alt1"><span class='item'>9/15/2011</span><span class='desc'><a href="/link/portal/8202/8390/Article/227/What-is-the-status-of-my-special-circumstances-request-My-financial-situation-has-not-changed" class=contentLinks >What is the status of my special circumstances request?  My financial situation has not changed.</a></span></li>
    <li class="alt2"><span class='item'>9/15/2011</span><span class='desc'><a href="/link/portal/8202/8390/Article/226/The-income-used-on-my-FAFSA-has-greatly-changed-from-that-tax-year-to-this-Is-there-anything-I-can-do" class=contentLinks >The income used on my FAFSA has greatly changed from that tax year to this.  Is there anything I can do?</a></span></li>
    <li class="alt1"><span class='item'>9/15/2011</span><span class='desc'><a href="/link/portal/8202/8390/Article/225/If-I-receive-a-scholarship-s-can-I-still-get-financial-aid-funding" class=contentLinks >If I receive a scholarship(s) can I still get financial aid funding?</a></span></li>
  </ol>

1 个答案:

答案 0 :(得分:0)

您在该页面上的代码如下所示:

jQuery(document).ready(function ($) {
    if (!$.trim($('h1').html()).length) {
        $('h1').hide();
    };
});

var highestCol = $("ol.mostrecent").height();
console.log(highestCol)
console.log("here");

测量高度的代码位于(document).ready块之外 - 您正在尝试在渲染DOM之前计算其高度。这应该有效:

jQuery(document).ready(function ($) {
    if (!$.trim($('h1').html()).length) {
        $('h1').hide();
    };

    var highestCol = $("ol.mostrecent").height();
    console.log(highestCol)
    console.log("here");
});