javascript - 希望两个 div 在页面加载时都设置为它们的最大高度

标签 javascript jquery html css

客户正在使用内容管理系统将文本注入(inject)代表按钮的 2 个 div。我希望按钮的高度相等。问题是:

  • 如果我使用 CSS 将两者的高度设置为相等,那么只有在客户端没有输入太多文本的情况下才有效。
  • 如果我不使用 CSS 设置高度,那么只有当客户端在两者中输入相同数量的文本行时才有效。

所以我认为 JQuery 会在这方面发挥作用。你能帮我弄清楚要使用哪些功能吗?

问题的概要是:

<div id="button1">
    <p>Here's some text that the client will control.</p>
</div>
<div id="button2">
    <p>Here's some more text that the client will control</p>
</div>

<script type="text/javascript">
     /* Here will go some JS to make button1 and button2 the height of whichever one is taller when the page has loaded the client's text that the CMS injected */
</script>

最佳答案

没有 JQuery...让它们成为 display: table-cell 元素,它们将自动匹配彼此的高度...

#button1, #button2 {
  display: table-cell;
  width: 50%;
  border: 1px solid red;
}
<div id="button1">
    <p>Here's some text that the client will control.</p>
</div>
<div id="button2">
    <p>Here's some more text that the client will control Here's some more text that the client will control Here's some more text that the client will control Here's some more text that the client will control</p>
</div>

关于javascript - 希望两个 div 在页面加载时都设置为它们的最大高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26005215/

相关文章:

html - 通过 css 应用 img-fluid 或普遍适用于数据库提供的图像

html - 没有办法在固定高度的 div 内垂直对齐文本吗?

javascript - 如何删除 Ext JS 文本字段显示的值?

javascript - 为什么我的网站在 IE 中是空白屏幕? (包括 IE9!)

javascript - 运行ajax请求时无法将内部html设置为null?

jquery - 模式弹出窗口中的引导时钟选择器自动调整问题

javascript - Python:抓取 Javascript 行

jquery - jQuery slider 上的第一张图片不合适

javascript - 图片库预览在 Chrome 和 Safari 中无法正确显示

html - 在一些 HTML 标签中看到的 "generated"属性是做什么用的?