div 的调整大小被忽略 [英] Resize of div is being ignored

查看:26
本文介绍了div 的调整大小被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个超级简单的网页,它将数据从服务器加载到作为容器的 s 中.问题是有时图像太高,溢出并导致布局问题.因此,我希望使用以下方法调整 div 的大小:

I have a super-simple web page that loads data from a server into s as containers. The problem is that sometimes images are too tall, and overflow and cause layout problems. So, I was looking to resize the divs using:

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

但到目前为止,我尝试过的一切都没有奏效.这是 html 文件:

But so far NOTHING I've tried has worked. Here's the html file:

    <!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>

就是这样!

我发现了各种类似这篇的文章,或者这个,但是,它们没有帮助.

I have found various articles like this one, or this one, but, they weren't helpful.

我突然想到,在我的情况下,我不需要使用 div 本身.我只需要图像和相关文本不要垂直(或水平)重叠.事实上,当图像加载时,它可能是一团糟....我怎么能告诉我的 div 是全宽的,没有其他 div 的覆盖?我尝试使用类、使用 CSS 等等,但都被忽略了.

It occurs to me to mention that in my case I don't require the use of divs per se. I just need the images and related text to not overlap vertically (or horizontally). As it is, when the images load, it can be a real mess. ...How can I tell my divs to be full width with no overlay from the other divs? I tried using a class, using CSS, and more, all ignored.

我想我是在不正确的假设下工作的,即 div 作为面向块的事物,即使您声明全宽(宽度 = 100%")也可以并且仍然会垂直重叠,具体取决于放置在

I guess I was working under the improper assumption that divs, as block-oriented things, that even if you declare full width ("width=100%") can and will STILL overlap vertically depending on the contents placed in them.

推荐答案

您没有显示分配给高度"的内容,但从您的代码中我假设它是一个整数.

you didnt show what you assigned to 'height', but from your code i assume its an interger.

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

style 需要一个字符串(即 10px),所以给它赋值是行不通的,试试下面的方法

style takes a string (ie 10px), so assigning number to it won't work, try the following

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

javascript 会为您将其转换为字符串
更多关于整数到字符串这里 在 JavaScript 中将数字转换为字符串的最佳方法是什么?

javascript will convert it to string for you
more on integer to string here What's the best way to convert a number to a string in JavaScript?

这篇关于div 的调整大小被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆