javascript - .css() jquery 无法正常工作

标签 javascript jquery css

我试图将窗口的高度应用于特定 div 的最大高度。

$(document).ready(function(){
        console.log('Initial Window height:', $(window).height());
        $('.wordboard').css({ "max-height": ($(window).height()-150) + 'px' });
        //console.log($('.wordboard').css({"max-height": ($(window).height() - 150) + 'px'}));


        console.log("Initial max height of $('.wordboard'): ", $('.wordboard').css("max-height"));
        $(window).resize(function(){
            $('.wordboard').css({ "max-height": ($('body').height()-150) + 'px' });
            console.log("$('.wordboard').css('max-height')",$('.wordboard').css('max-height'));
        });

    });

但是,我的网站已加载:未应用最大高度。控制台中的结果是加载网站时:

Initial Window height: 550
script.js:28 Initial max height of $('.wordboard'):  undefined
(After i resize the window, the effect is now apply. However, I want to apply as soon as the window is loaded)

script.js:31 $('.wordboard').css('max-height') 718px
script.js:31 $('.wordboard').css('max-height') 723px
script.js:31 $('.wordboard').css('max-height') 728px
script.js:31 $('.wordboard').css('max-height') 733px

最佳答案

想到的唯一解决方案是,当您在文档就绪伪事件上调用代码段时,您的元素 .wordboard 不存在于 DOM 中。您肯定(或插件)动态添加它。

编辑:检查一下:

$(function(){console.log($(".wordboard").length);});

如果显示 0,您就知道问题出在哪里。

关于javascript - .css() jquery 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30640811/

相关文章:

javascript - 主干事件没有捕捉到对 div 的点击

javascript - ng-repeat 中的花括号 - 语法?

jquery - Twitter Bootstrap 轮播 : Slides All Showing Simultaneously

javascript - js获取横向滚动事件

css - @font-face 在 Chrome/IE 中渲染更大,fontsquirrel

javascript - 试图在悬停时使不同的文本出现在 3 个对齐的图像下方

javascript - 在类变量 JavaScript 上设置 getter/setter

php - 使用 php Ajax 将 json 发送到服务

html - div 内垂直居中

css - 右侧具有可变高度 div 的全尺寸背景图像