【问题标题】:How to assign JavaScript value to a CSS property? [duplicate]如何将 JavaScript 值分配给 CSS 属性? [复制]
【发布时间】:2015-01-27 16:02:39
【问题描述】:

我有一个 div 元素:

<div class="page">
        //some contents
</div>

CSS:

.page{
    bottom: 0;
    color: #1f1f21;
    font-size: 17px;
    font-weight: 400;
    left: 0;
    overflow: visible;
    position: absolute;
    right: 0;
    top: 0;
}

使用 JavaScript 获取屏幕高度:

var page_height = screen.height;
alert(page_height);

如何将此 page_height 值应用于我的 .page 类?

(即)我需要将屏幕高度设置为我的 page

例如,如果screen.height = 405

我需要添加高度属性

height: 405px;

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    使用.height():

    $('.page').height(page_height);
    

    【讨论】:

    • downvoter 关心评论??
    • 工作:) 谢谢..会接受你的回答
    • @UIDesigner:不知道:/
    【解决方案2】:

    试试这个Fiddle

    var page_height = screen.height;
    $('.page').height(page_height)
    alert(page_height);
    .page{
        bottom: 0;
        color: #1f1f21;
        font-size: 17px;
        font-weight: 400;
        left: 0;
        overflow: visible;
        position: absolute;
        right: 0;
        top: 0;
        width:500px;
        background:green;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
    <div class="page">
            //some contents
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-19
      • 2019-03-20
      • 2014-06-11
      • 2019-04-03
      • 2014-01-16
      相关资源
      最近更新 更多