代码之家  ›  专栏  ›  技术社区  ›  Richard T

正在忽略调整div的大小

  •  1
  • Richard T  · 技术社区  · 6 年前

    target.style.height = height; // As short form of:
    document.getElementById('someId').style.height=height;
    

    但到目前为止我试过的都没用。以下是html文件:

        <!DOCTYPE html>
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        </head>
        <body bgcolor="#E3FBFF" text="#000000" onload="loadImages();">
          <script type = "text/javascript">
            function loadImages()
            {
              ...Function omitted for brevity...
              But it gets to this line:
              document.getElementById(d0).style.height = height+10;
            }
      </script>
        <div id=d0></div>
        <hr>
        <div id=d1></div>
        <hr>
        <div id=d2></div>
        <hr>
        <div id=d3></div>
        <hr>
        </body>
        </html>
    

    就这样!

    我找到了各种各样的文章 like this one this one

    我突然想到,在我的情况下,我不需要使用div本身。我只需要图像和相关的文字不重叠垂直(或水平)。事实上,当图像加载时,它可能是一个真正的混乱。。。我怎样才能告诉我的div是全宽的,没有其他div的重叠?我试着用一个类,用CSS等等,都忽略了。

    2 回复  |  直到 4 年前
        1
  •  2
  •   Chris Li    6 年前

    您没有显示指定给“height”的内容,但从代码中我假设它是一个整数。

    document.getElementById(d0).style.height = height+10;
    

    style需要一个字符串(即10px),因此为其分配数字将不起作用,请尝试以下操作

    document.getElementById(d0).style.height = height+10+'px';
    

    javascript将它转换为字符串
    更多关于整数字符串的信息 What's the best way to convert a number to a string in JavaScript?

        2
  •  2
  •   Frish    6 年前

    height 变量。在编辑元素的CSS样式时,这是必需的。请参见: MDN

    document.getElementById('someId').style.height=height 变成: document.getElementById('someId').style.height = height.toString() + 'px'