简体   繁体   English

jQuery不适用于css属性

[英]jquery doesn't apply css property

I'm getting crazy about a small function that I'm writing. 我为正在编写的一个小函数而疯狂。

This function need to set the height of a container equal to the highest UL (3 UL in total and with absolute position) present inside it and, if the second or third UL are lower than the first one, set the height of these UL equal to the first one. 此功能需要将容器的高度设置为等于其内部存在的最高UL(总计3个UL,并且绝对位置),并且如果第二个或第三个UL低于第一个UL,则将这些UL的高度设置为相等到第一个。

This is the function: 这是功能:

function fixNavHeight() {
    var cont = white,
        arr = [];
        lis = $('nav li');

    lis.each(function() {
        var li = $(this);
        li.mouseenter(function() {
            var childrenHeight = $(this).find(secondaryNavigation).height();
            cont.css('height', childrenHeight);
            setHeight(childrenHeight);
        });
    });

    function setHeight(base) {
        arr = [];
        cont.find('ul').each(function() {
            if($(this).is(':visible')) {
                var h = $(this).height();
                console.log(h);
                if(h <= base) {
                    $(this).css('height', base);
                }
                arr.push(h);
                var biggest = Math.max.apply(null, arr);
                cont.css('height', biggest);
            }
        });
    }
}

Everything works fine but if the UL is lower than base it doesn't set the css height and I can not understand why. 一切正常,但如果UL低于base ,则不会设置CSS高度,我不明白为什么。

Can you help me to find the issue? 您能帮我找到问题吗?

What I'm doing wrong here? 我在这里做错了什么?

When applying height or width with .css() add the px suffix, like this: 当使用.css()应用高度或宽度时,请添加px后缀,如下所示:

cont.css('height', biggest +'px');

Do the same to all your similar calls. 对所有类似的呼叫执行相同的操作。

Alternatively if you want to use your current syntax with just the number, then pass the css properties as an object to .css() , like this: 另外,如果您想仅使用数字使用当前语法,则将css属性作为对象传递给.css() ,如下所示:

cont.css({'height': biggest});

Try to console.log these items inside the mouseenter callback: 尝试在mouseenter回调中使用console.log这些项目:

secondaryNavigation
$(this).find(secondaryNavigation)
$(this).find(secondaryNavigation).height()

Check that secondaryNavigation is defined. 检查是否已定义secondaryNavigation It comes from nowhere in the code (probably some lines above) and it must be accessoible. 它来自代码中的任何地方(可能在上面的几行中),并且必须可访问。

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

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