Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.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
计数<;ul>;使用Javascript的高度_Javascript_Jquery_Jquery Plugins_Height - Fatal编程技术网

计数<;ul>;使用Javascript的高度

计数<;ul>;使用Javascript的高度,javascript,jquery,jquery-plugins,height,Javascript,Jquery,Jquery Plugins,Height,这是我的情况。我有一个滑动jquery菜单,如下所示: 菜单代码为: <div id="sbi_container" class="sbi_container"> <div class="sbi_panel" data-bg="images/1.jpg"> <a href="#" class="sbi_label">About</a> <div class="sbi_conten

这是我的情况。我有一个滑动jquery菜单,如下所示:

菜单代码为:

<div id="sbi_container" class="sbi_container">
        <div class="sbi_panel" data-bg="images/1.jpg">
            <a href="#" class="sbi_label">About</a>
            <div class="sbi_content">
                <ul>
                    <li><a href="#">Subitem</a></li>
                    <li><a href="#">Subitem</a></li>
                    <li><a href="#">Subitem</a></li>
                </ul>
            </div>
        </div>
        <div class="sbi_panel" data-bg="images/2.jpg">
            ...
        </div>
这里有一个JavaScript片段我有一个问题:

    $content.each(function(i) {
            var $el = $(this);
// save each content's height (where the menu items are)
// and hide them by setting the height to 0px
            var num = $el.find('ul li').size();

            $el.data( 'height', num * 35 + 10 ).css( 'height', '0px' ).show();
                    });
我需要计算这些
的高度。 我现在做的是计算有多少个
  • 项目,然后乘以一个人的身高(35),再加上10像素的边距

    一切正常,但当有一个真正长的
  • 项目时,它会跳到第二行,问题是这个代码不计算第二行的高度

    有什么办法可以解决这个问题吗


    谢谢。

    您可以使用
    scrollHeight
    属性:

    $("ul")[0].scrollHeight
    
    就你的个案而言—

    $content.find('ul')[0].scrollHeight
    

    您可以使用
    scrollHeight
    属性:

    $("ul")[0].scrollHeight
    
    就你的个案而言—

    $content.find('ul')[0].scrollHeight
    

    您可以使用以下方法计算内容中所有
    li
    s的高度:

    var totalLiHeight = 0;
    $el.find('ul li').each(function() {
        var currentLiHeight = $(this).height();
        totalLiHeight += currentLiHeight;
    });
    

    然后,您可以添加边距。

    您可以使用以下方法计算内容中所有
    li
    s的高度:

    var totalLiHeight = 0;
    $el.find('ul li').each(function() {
        var currentLiHeight = $(this).height();
        totalLiHeight += currentLiHeight;
    });
    

    然后你可以在上面加上边距。

    你试过了吗:
    $(“#你的ul”)。outerHeight()
    是的,我试过了,然后把事情搞清楚。您可以在这里看到:当
  • 项中有多个单词时,id会在子菜单下面添加一个单独的空格。。我真的不知道为什么……你试过了吗:
    $(“#你的ul”).outerHeight()
    是的,我试过了,然后就开始了。您可以在这里看到:当
  • 项中有多个单词时,id会在子菜单下面添加一个单独的空格。。我真的不知道为什么..当写入:
    var num=$el.find('ul')[0]时num=0对我来说很重要。为什么?这有点奇怪,因为我能找到的所有东西都是:
    var$el=$(这个)(您可以在我的主要帖子的第一行Java脚本中看到它)。或者这不是吗?
    var$content=$el.find('div.sbi_content')
    您不需要$content.each()。。。您可以直接执行$content.find('ul')[0]。滚动高度。。。实际上,只需执行$('div.sbi_content ul')[0]。滚动高度以获取ulHmm的高度。。我开始理解了。但是,如果我最终需要获取
    $el.data('height','number').css('height','0px').show();),我应该如何编写所有这些内容呢?写入时:
    var num=$el.find('ul')[0]。滚动高度num=0对我来说很重要。为什么?这有点奇怪,因为我能找到的所有东西都是:
    var$el=$(这个)(您可以在我的主要帖子的第一行Java脚本中看到它)。或者这不是吗?
    var$content=$el.find('div.sbi_content')
    您不需要$content.each()。。。您可以直接执行$content.find('ul')[0]。滚动高度。。。实际上,只需执行$('div.sbi_content ul')[0]。滚动高度以获取ulHmm的高度。。我开始理解了。但是,如果我最终需要获取
    $el.data('height','number').css('height','0px').show();),我应该如何编写所有这些内容呢