【问题标题】:How to change the width of the html selector with javascript如何使用 javascript 更改 html 选择器的宽度
【发布时间】:2014-03-09 12:31:15
【问题描述】:

在我的 CSS 中,我这样设置 html 的宽度:

html {width: 1000px; }

我希望能够使用 javascript 动态更改此宽度,类似于设置元素的 style.width。 window.document 节点没有样式属性,我在任何地方都找不到宽度。

1000px 存储在 DOM 中的什么位置,我可以动态更改它吗?

【问题讨论】:

    标签: javascript html css dom


    【解决方案1】:

    你总是可以得到它:

    document.documentElement
    

    或:

    document.getElementsByTagName('html')[0]
    

    甚至:

    document.body.parentElement
    

    所以要获得 CSS 宽度,只需:

    document.documentElement.style.width
    

    文档:https://developer.mozilla.org/en-US/docs/Web/API/document.documentElement

    返回作为文档根元素的元素(例如,HTML 文档的 元素)。

    【讨论】:

    • 完美!就是这样。谢谢你,Shomz。
    【解决方案2】:

    我个人喜欢这种方式

    document.body.parentNode.style.width = "1000px";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-27
      • 2013-08-01
      • 2012-04-21
      • 1970-01-01
      • 2019-08-22
      相关资源
      最近更新 更多