简体   繁体   English

jQuery:在页面加载时设置自动高度

[英]jQuery: Set auto height on page load

I need the class inside of the <li> tag to be auto height/adjustable to its <li> , however it wasn't possible with CSS( height:auto;height:100% and other options) so I tried jQuery but I'm a newbie so I tried what I've learned so far. 我需要<li>标记内的class能够自动调整高度/使其适应<li> ,但是CSS( height:auto;height:100%和其他选项)不可能,所以我尝试了jQuery但我是新手,所以我尝试了到目前为止所学的知识。
The following code does just what I need but I wasn't able to figure out how to set the auto height for the class on page load instead of .click function. 以下代码可以满足我的需要,但我无法弄清楚如何在页面加载时设置类的自动高度,而不是.click函数。 Please note that I have many <li> tags with different sizes so (this) needs to be included. 请注意,我有许多大小不同的<li>标记,因此需要包含(this)

$j("#accordion ul li").click(function () { 
  var current = $j(this).height() - 2;
  $j(this).find(".status-green").attr("style","height:" + current + "px");
});

Thanks in advance 提前致谢

$j(function () {
    $('#accordion').find('.status-green').each(function (index, value) {
        var $value = $j(value),
            h      = $value.parents('li').height() - 2;
        $value.height(h);
    });
});

This will find and iterate through each .status-green element within the #accordion element when the DOM is ready ( $j(function(){}); is short-hand for $j(document).ready(function(){}); ). 当DOM准备就绪时( $j(function(){});$j(document).ready(function(){});简写形式$j(function(){}); #accordion#accordion元素中的每个.status-green元素中查找和迭代$j(function(){}); $j(document).ready(function(){}); )。

--Update-- -更新-

This more closely resembles your code: 这更类似于您的代码:

$j(function ($) {
    $("#accordion").find('li').each(function (index, value) { 
        var $value  = $(value),
            current = $value.height() - 2;
        $value.find(".status-green").height(current);
    });
});

If you pass $ as a argument in the first line then you can use $() rather than $j() . 如果在第一行中将$作为参数传递,则可以使用$()而不是$j() Also the .height() function assumes pixels if only a number is passed to it. 如果仅将数字传递给.height()函数,该函数.height()假定像素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM