javascript - 根据预设 "div"和 "width"的内容动态更改 "height"的高度

标签 javascript jquery html css ajax

我有一个带有预设“宽度”和“高度”的“div”。我需要根据内容更改该“div”的高度。我一直在寻找一些 JavaScript,但找不到适合我的。

这是我的简单示例,需要改进http://jsfiddle.net/dmitry313/1zr6Lhwa/

HTML

<div id="content">
<h2>Text</h2>
<p>Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example </p> 
</div> 

CSS

    h2 {
font:18px/30px Arial;
}   
#content {
width:200px;
height:100px;
border:1px solid #000;
background: yellow;
}

编辑:最初预设的“宽度”和“高度”很重要,不能更改

最佳答案

只需从 CSS 中删除 height。如果您需要预设高度,请改用 min-height。那个怎么样:

h2 {
  font: 18px/30px Arial;
}

.content {
  width: 200px;
  min-height: 100px;
  border: 1px solid #000;
  background: yellow;
}
<div class="content">
<h2>Text</h2>
<p>Example Example</p>  
</div>   
<br>
<div class="content">
<h2>Text</h2>
<p>Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example </p>  
</div>

编辑:如果你想保持容器的高度并显示可滚动的内容,使用overflow: auto;

h2 {
  font: 18px/30px Arial;
}

.content {
  width: 200px;
  height: 100px;
  overflow: auto;
  border: 1px solid #000;
  background: yellow;
}
<div class="content">
<h2>Text</h2>
<p>Example Example</p>  
</div>   
<br>
<div class="content">
<h2>Text</h2>
<p>Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example </p>  
</div>

关于javascript - 根据预设 "div"和 "width"的内容动态更改 "height"的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25934581/

相关文章:

javascript - YouTube 观看稍后看不到的所有内容 (javascript)

javascript - 错误 TypeError : _v. context.$implicit.toggle 不是函数

javascript - $.get() 加载的图像不显示

php - 如何从 HTML 标签中读取 CDATA

javascript - 设置 style.display = none 时不显示 <li> 标签

Javascript::在闭包中生成新变量并通过对象实例使用它

javascript - 如何使动画关闭与打开相同

javascript - jQuery 删除或隐藏 Canvas 上的所有 svg

javascript - 在悬停时隐藏图像标题并显示叠加文本

javascript - 通过 Nashorn javascript jjs 进行 JMX 连接