如何将样式应用于 JavaScript 中的类?

发布于 12-23 08:54 字数 715 浏览 1 评论 0原文

我的身体中有多个需要相同样式的 div:

<div class="box"></div>

我有一个 javascript 可以计算浏览器窗口视口高度的高度。 我想将该高度应用于所有“box”类 div。

<script type="text/javascript">
var box = document.getElementsByClassName('box')[0];
var height = (window.innerHeight) ? window.innerHeight: document.documentElement.clientHeight;
box.style.height=(height)+'px';
</script>

这有效,但仅适用于一个 div,它不会使第二个“盒子”div 与前一个 div 具有相同的高度。如果我将方括号中的数字从 0 更改为 1,则会将高度应用于第二个 div。另外,如果我制作 javascript 的第二个副本,以便第一个脚本具有 [0] 和第二个 [1] ,它将高度应用于两个 div。我无法删除方括号,因为那样 JavaScript 就根本不起作用。我还尝试使用 getElementsByTagName 获取名称,但没有成功。

我怎样才能让这个javascript对所有具有“box”类的div应用相同的高度?或者还有另一种方法可以做我想做的事情吗?

I have multiple divs in my body which need the same style:

<div class="box"></div>

I have a javascript that calculates the height of the browser window viewport height.
I want to apply that height to all my "box" classed divs.

<script type="text/javascript">
var box = document.getElementsByClassName('box')[0];
var height = (window.innerHeight) ? window.innerHeight: document.documentElement.clientHeight;
box.style.height=(height)+'px';
</script>

This works, but only with one div, it doesn't make the second "box" div the same height as the previous. If I change the number in square brackets from 0 to 1 it applies the height to the second div. Also, if I make a second copy of the javascript so that the first script has [0] and the second [1] it applies the height to both divs. I can't delete the square brackets because then the javascript doesn't work at all. I have also tried to get the name with getElementsByTagName with no success.

How can I make this javascript apply the same height to all the divs with "box" class? Or is there another way to do what I try to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半衾梦2024-12-30 08:54:02

尝试以下操作:

var elems = document.getElementsByClassName('box'),
    size = elems.length;

for (var i = 0; i < size; i++) {

var box = elems[i];
var height = (window.innerHeight) ? window.innerHeight: document.documentElement.clientHeight;
box.style.height=(height)+'px';
}

演示: http://jsfiddle.net/JRdHD/

try the following:

var elems = document.getElementsByClassName('box'),
    size = elems.length;

for (var i = 0; i < size; i++) {

var box = elems[i];
var height = (window.innerHeight) ? window.innerHeight: document.documentElement.clientHeight;
box.style.height=(height)+'px';
}

Demo: http://jsfiddle.net/JRdHD/

浅暮の光2024-12-30 08:54:02

一个简单的方法是在 html 中 document.write 样式,
在阅读页面时,在定义链接元素之后。

<script>
document.writeln('<style>div.box{height:'+
document.documentElement.clientHeight+'px;}'+
'<\/style>');
</script>
</head>

<body...

A simple way would be to document.write the style in the html,
while the page is being read, after the link elements are defined.

<script>
document.writeln('<style>div.box{height:'+
document.documentElement.clientHeight+'px;}'+
'<\/style>');
</script>
</head>

<body...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文