简体   繁体   English

jquery css高度选择器?

[英]jquery css height selector?

i want to set equal height in div which have class name "modelCode" so i made a small function to do so but when i am running this function it is setting height 0px in elemeny style. 我想在div中设置具有类名“modelCode”的相等高度,所以我做了一个小函数,但是当我运行这个函数时,它是以elemeny风格设置高度0px。 Before that i used .height() method which is working perfactly but i want to this with css height method any guess. 在此之前,我使用.height()方法,这是正常工作但我想用css高度方法任何猜测。 Thanks in advance 提前致谢

<head>
    <script type="text/javascript">
    function equalHeight(group) {
        var tallest = 0;
        group.each(function () {
            var thisHeight = $(this).css('height');
                if (thisHeight > tallest) {
                    tallest = thisHeight;
                }
         });
         group.css('height',tallest);
    }

    function setequalheight() {
        equalHeight($(".modelCode"));
    }

    $(document).ready(function () { setequalheight() });
    </script>
<style>
    .modelCode { width:200px; float:left; border:solid 1px #F00}
</style>
</head>
<body>
    <div class="modelCode">i am new text </div>
    <div class="modelCode">i am new text <br />i am new text <br />i am new text <br />i am new text <br /></div>

it will work now 它现在会起作用

function equalHeight(group) {
var tallest = 0;
group.each(function () {
    var thisHeight = $(this).css('height');
    thisHeight = parseInt(thisHeight.substr(0 , (thisHeight.length - 2)));
    //alert(thisHeight)
    if (thisHeight > tallest) {
        tallest = thisHeight;
    }


});

group.css('height',tallest);
}

the problem is that .css('height') returns a string value, so you have to parse it to integer to compare which is bigger value 问题是.css('height')返回一个字符串值,所以你必须将它解析为整数来比较哪个更大的值

When you use jQuery's css method you have to provide the entire value. 当您使用jQuery的css方法时,您必须提供整个值。 In this case you provide it with some integer, when really you want to give it the integer plus "px". 在这种情况下,你提供一些整数,当你真的要给它整数加“px”。 It's the same as writing "height:40" in css (if the integer is 40). 它与在css中写入“height:40”相同(如果整数为40)。

Try changing 尝试改变

group.css('height',tallest)

to

group.css('height',tallest+"px");

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

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