javascript - 如何在 css 表中使用 js 文件中的数字?

标签 javascript html css

我想在 css 表中使用 js 中计算的数字。

我有这个 js:

<script type="text/javascript">

//js cut out here

//document.write('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>');
alert('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>');

//-->
</script>

有警告,其中 viewportwidth 显示浏览器的宽度,viewportheight 显示浏览器窗口的高度。我想使用这些数字是这样的样式表:

<style type="text/css">
html, body, 
#page {
    width:   100%;
    height:  100%;
    margin:  0;
}
#map_canvas {
    width:   (viewportwidth-200) px;
    height:  viewportheight;
}
#sidebar {
    width:200px;
    height:viewportheight px;
    overflow:auto;
}
</style>

这有可能吗?

我找到了这个:Parse Javascript variable to CSS variables

但我无法让它工作

最佳答案

您可以使用 JavaScript 修改特定元素的样式:

document.getElementById('map_canvas').style.width = "800px";

关于javascript - 如何在 css 表中使用 js 文件中的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12041331/

相关文章:

php - 在表单中追加新元素并在 PHP 中发布其值并将其保存在数据库中

javascript - MVC - 无法加载资源 : the server responded with a status of 500 (Internal Server Error)

javascript - 删除数组对象 es6 中的属性

html - IE7 不在无序列表上显示列表样式图像?

javascript - 重新计算样式 : why so stuttering?

javascript - 是否可以通过将参数传递给函数来将函数中的数据存储到对象文字?

javascript - 我需要从文本框中动态打印文本。这是从 mysql 加载的。

html - Paper-Tabs 选择栏不显示

html - 使所有 div child 具有相同的高度

javascript - 通过jquery html()设置html的缺点